- 浏览: 3368210 次
- 性别:
- 来自: 珠海
文章分类
- 全部博客 (1633)
- Java (250)
- Android&HTML5 (111)
- Struts (10)
- Spring (236)
- Hibernate&MyBatis (115)
- SSH (49)
- jQuery插件收集 (55)
- Javascript (145)
- PHP (77)
- REST&WebService (18)
- BIRT (27)
- .NET (7)
- Database (105)
- 设计模式 (16)
- 自动化和测试 (19)
- Maven&Ant (43)
- 工作流 (36)
- 开源应用 (156)
- 其他 (16)
- 前台&美工 (119)
- 工作积累 (0)
- OS&Docker (83)
- Python&爬虫 (28)
- 工具软件 (157)
- 问题收集 (61)
- OFbiz (6)
- noSQL (12)
最新评论
-
HEZR曾嶸:
你好博主,这个不是很理解,能解释一下嘛//左边+1,上边+1, ...
java 两字符串相似度计算算法 -
天使建站:
写得不错,可以看这里,和这里的这篇文章一起看,有 ...
jquery 遍历对象、数组、集合 -
xue88ming:
很有用,谢谢
@PathVariable映射出现错误: Name for argument type -
jnjeC:
厉害,困扰了我很久
MyBatis排序时使用order by 动态参数时需要注意,用$而不是# -
TopLongMan:
非常好,很实用啊。。
PostgreSQL递归查询实现树状结构查询
注意: 下面的配置不是拦截Controller的, 拦截Controller的需要在servlet.xml, 在扫描Controller的后面加入, 如果是拦截service,那么应该放在扫描service注解的context.xml:
其中拦截器代码如下:
以上的代码亲自测试成功, 试了一天, 到晚上11点半要放弃,然后准备睡觉的之前才试出来的.
Spring AOP中pointcut expression表达式解析
http://blog.csdn.net/kkdelta/article/details/7441829
Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的.
Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合.
args()
@args()
execution()
this()
target()
@target()
within()
@within()
@annotation
其中execution 是用的最多的,其格式为:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern是必须的.
ret-type-pattern:可以为*表示任何返回值,全路径的类名等.
name-pattern:指定方法名,*代表所以,set*,代表以set开头的所有方法.
parameters pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.
举例说明:
任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))
pointcutexp包里的任意类.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意类.
within(com.test.spring.aop.pointcutexp..*)
实现了Intf接口的所有类,如果Intf不是接口,限定Intf单个类.
this(com.test.spring.aop.pointcutexp.Intf)
***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.
带有@Transactional标注的所有类的任意方法.
@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)
带有@Transactional标注的任意方法.
@annotation(org.springframework.transaction.annotation.Transactional)
***> @within和@target针对类的注解,@annotation是针对方法的注解
参数带有@Transactional标注的方法.
@args(org.springframework.transaction.annotation.Transactional)
参数为String类型(运行是决定)的方法.
args(String)
Pointcut 可以通过Java注解和XML两种方式配置,如下所示:
例子:
MethodInterceptor --> org.springframework.aop.support.RegexpMethodPointcutAdvisor
ThrowsAdvice --> org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
http://hi.baidu.com/loving102/blog/item/bf1395d3d9ddbd093bf3cfaf.html
实现MethodInterceptor接口,在调用目标对象的方法时,就可以实现在调用方法之前、调用方法过程中、调用方法之后对其进行控制。
MethodInterceptor接口可以实现MethodBeforeAdvice接口、AfterReturningAdvice接口、ThrowsAdvice接口这三个接口能够所能够实现的功能,但是应该谨慎使用MethodInterceptor接口,很可能因为一时的疏忽忘记最重要的MethodInvocation而造成对目标对象方法调用失效,或者不能达到预期的设想。
关于含有Advice的三种对目标对象的方法的增强
例子参考: http://www.360doc.com/content/07/0827/10/18042_697579.shtml
源代码
---------------------------------------------
MyMethodInterceptor.java
注:注释掉的那部分,有问题,一定要用try{......}finally{......}的方式来使用,否则可能会出问题。比如注释的那部分,只能打印第一个system.out.println(),不能打印第二个的!
applicationContext.xml
测试例子:
结果显示:
Proceed start time:Mon Jul 18 10:15:27 CST 2011
doAnotherThing-->
Proceed end time:Mon Jul 18 10:15:27 CST 2011
<bean id="springMethodInterceptor" class="cn.com.voge.base.comm.RhModulePriCheckInterceptor" ></bean> <aop:config proxy-target-class="true"> <aop:pointcut id="loginPoint" expression="execution(public * com.ronghuitec.pm.controller.LoginController.*(..)) "/> <aop:advisor pointcut-ref="loginPoint" advice-ref="springMethodInterceptor"/> </aop:config>
其中拦截器代码如下:
package cn.com.voge.base.comm; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import javax.servlet.http.HttpServletRequest; /** * 项目名称: wp_idea_linux * 功能说明: * 创建者: Pandy, * 邮箱: panyongzheng@163.com, 1453261799@qq.com * 版权: * 官网: * 创建日期: 15-8-10. * 创建时间: 下午1:03. * 修改历史: * ----------------------------------------------- */ public class RhModulePriCheckInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { Object[] ars = methodInvocation.getArguments(); if(ars!=null){ for(Object o :ars){ if(o instanceof HttpServletRequest){ System.out.println("------------this is a HttpServletRequest Parameter------------ "); } } } // 判断该方法是否加了@LoginRequired 注解 if(methodInvocation.getMethod().isAnnotationPresent(RhModulePriCheck.class)){ System.out.println("----------this method is added @LoginRequired-------------------------"); } return methodInvocation.proceed(); } }
以上的代码亲自测试成功, 试了一天, 到晚上11点半要放弃,然后准备睡觉的之前才试出来的.
Spring AOP中pointcut expression表达式解析
http://blog.csdn.net/kkdelta/article/details/7441829
Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的.
Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合.
args()
@args()
execution()
this()
target()
@target()
within()
@within()
@annotation
其中execution 是用的最多的,其格式为:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern是必须的.
ret-type-pattern:可以为*表示任何返回值,全路径的类名等.
name-pattern:指定方法名,*代表所以,set*,代表以set开头的所有方法.
parameters pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.
举例说明:
任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))
pointcutexp包里的任意类.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意类.
within(com.test.spring.aop.pointcutexp..*)
实现了Intf接口的所有类,如果Intf不是接口,限定Intf单个类.
this(com.test.spring.aop.pointcutexp.Intf)
***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.
带有@Transactional标注的所有类的任意方法.
@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)
带有@Transactional标注的任意方法.
@annotation(org.springframework.transaction.annotation.Transactional)
***> @within和@target针对类的注解,@annotation是针对方法的注解
参数带有@Transactional标注的方法.
@args(org.springframework.transaction.annotation.Transactional)
参数为String类型(运行是决定)的方法.
args(String)
Pointcut 可以通过Java注解和XML两种方式配置,如下所示:
<aop:config> <aop:aspectrefaop:aspectref="aspectDef"> <aop:pointcutidaop:pointcutid="pointcut1"expression="execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))"/> <aop:before pointcut-ref="pointcut1" method="beforeAdvice" /> </aop:aspect> </aop:config>
@Component @Aspect public class AspectDef { //@Pointcut("execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))") //@Pointcut("within(com.test.spring.aop.pointcutexp..*)") //@Pointcut("this(com.test.spring.aop.pointcutexp.Intf)") //@Pointcut("target(com.test.spring.aop.pointcutexp.Intf)") //@Pointcut("@within(org.springframework.transaction.annotation.Transactional)") //@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)") @Pointcut("args(String)") public void pointcut1() { } @Before(value = "pointcut1()") public void beforeAdvice() { System.out.println("pointcut1 @Before..."); }
例子:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:config> <aop:aspect id="TestAspect" ref="myInterceptor"> <aop:pointcut id="servicem" expression="execution(* com.cn.service.BusinessService.*(..))" /> <aop:before pointcut-ref="servicem" method="doAccessCheck"/> <aop:after pointcut-ref="servicem" method="after"/> <aop:around pointcut-ref="servicem" method="doBasicProfiling"/> <aop:after-throwing pointcut-ref="servicem" method="doAfterThrow" throwing="ex"/> </aop:aspect> </aop:config> <bean id="myInterceptor" class="com.cn.service.MyInterceptor"/> <bean id="service" class="com.cn.service.BusinessServiceImpl"></bean> </beans>
MethodInterceptor --> org.springframework.aop.support.RegexpMethodPointcutAdvisor
ThrowsAdvice --> org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
http://hi.baidu.com/loving102/blog/item/bf1395d3d9ddbd093bf3cfaf.html
实现MethodInterceptor接口,在调用目标对象的方法时,就可以实现在调用方法之前、调用方法过程中、调用方法之后对其进行控制。
MethodInterceptor接口可以实现MethodBeforeAdvice接口、AfterReturningAdvice接口、ThrowsAdvice接口这三个接口能够所能够实现的功能,但是应该谨慎使用MethodInterceptor接口,很可能因为一时的疏忽忘记最重要的MethodInvocation而造成对目标对象方法调用失效,或者不能达到预期的设想。
关于含有Advice的三种对目标对象的方法的增强
例子参考: http://www.360doc.com/content/07/0827/10/18042_697579.shtml
源代码
---------------------------------------------
MyMethodInterceptor.java
package com.app.aop; import java.util.Date; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyMethodInterceptor implements MethodInterceptor { /* public Object invoke(MethodInvocation invoke) throws Throwable { // TODO Auto-generated method stub System.out.println("Proceed start time:"+(new Date())); Object result = invoke.proceed(); System.out.println("Proceed end time:"+(new Date())); return result; }*/ public Object invoke(MethodInvocation invoke) throws Throwable { // TODO Auto-generated method stub System.out.println("\nProceed start time:"+(new Date())); try { Object result = invoke.proceed(); return result; }finally{ System.out.println("Proceed end time:"+(new Date())); } } }
注:注释掉的那部分,有问题,一定要用try{......}finally{......}的方式来使用,否则可能会出问题。比如注释的那部分,只能打印第一个system.out.println(),不能打印第二个的!
applicationContext.xml
<bean id="myMethodInterceptor" class="com.app.aop.MyMethodInterceptor"></bean> <bean id="timeHandlerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref bean="myMethodInterceptor"/> </property> <property name="patterns"> <value>com.app.aop.BizProcessImpl.*</value> </property> </bean>
测试例子:
package com.app.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AopTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //IBizProcessImpl t = ctx.getBean("bizOne",IBizProcessImpl.class); IBizProcessImpl t = ctx.getBean("bizOneTarget",IBizProcessImpl.class); try { t.doAnotherThing(); } catch (Exception e) { // TODO: handle exception } } }
结果显示:
Proceed start time:Mon Jul 18 10:15:27 CST 2011
doAnotherThing-->
Proceed end time:Mon Jul 18 10:15:27 CST 2011
发表评论
-
Spring Boot 属性配置
2016-06-24 11:04 1151Spring Boot 属性配置和使用 http://blog ... -
Spring Boot 集成MyBatis
2016-06-24 10:55 1994Spring Boot 集成MyBatis http://bl ... -
Spring MVC防重复提交
2016-06-17 15:47 1593http://my.oschina.net/zyqjustin ... -
Spring容器加载完之后执行特定任务
2016-06-17 15:36 2245http://my.oschina.net/simpleton ... -
使用spring-session和shiro来代理session的配置
2016-06-16 11:21 12000使用spring-session和redis来代理sessio ... -
JSTL 的 if else : 有 c:if 没有 else 的处理
2016-06-14 09:52 1296http://blog.csdn.net/xiyuan1999 ... -
spring mvc 请求转发和重定向
2016-06-14 09:48 1353http://blog.csdn.net/jackpk/art ... -
mvc:view-controller
2016-05-18 10:26 1050http://blog.csdn.net/lzwglory/a ... -
spring配置事物的方式:注解和aop配置
2016-05-14 00:26 4066参考: Spring AOP中pointcut express ... -
分布式任务调度组件 Uncode-Schedule
2016-05-13 14:47 2246http://www.oschina.net/p/uncode ... -
Mybatis分库分表扩展插件
2016-05-12 15:47 1589http://fangjialong.iteye.com/bl ... -
spring+mybatis+atomikos 实现JTA事务
2016-05-11 22:00 5476sping配置多个数据源 不同用户操作不同数据库 http:/ ... -
Spring中使用注解 @Scheduled执行定时任务
2016-05-10 09:39 1537原文:http://dwf07223.blog.51cto.c ... -
Spring中配置Websocket
2016-05-05 16:55 1249spring+websocket整合(springMVC+sp ... -
redis 集群中Session解决方案之Spring Session
2016-05-04 08:54 1277集群中Session解决方案之Spring Session h ... -
使用Spring-data进行Redis操作
2016-05-04 08:54 4737使用Spring-data进行Redis操作 http://z ... -
Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
2016-05-03 13:35 1032Spring4新特性——集成Bean Validation 1 ... -
SpringMVC介绍之Validation
2016-05-03 13:10 955SpringMVC介绍之Validation http://h ... -
spring 注解方式下使用commons-validator 验证表单
2016-05-03 11:08 3041原文: http://www.programgo.com/ar ... -
Spring MVC学习详解
2016-04-28 09:13 970原文 http://blog.csdn.net/alsocod ...
相关推荐
常用拦截 拦截器HandlerInterceptor 拦截器MethodInterceptor 添加依赖 创建启动类 创建拦截器类 创建控制器 监控control请求耗时,提高性能
2)、事务拦截器: TransactionInterceptor;保存了事务属性信息,事务管理器; 他是一个 MethodInterceptor; 在目标方法执行的时候; 执行拦截器链; 事务拦截器: 1)、先获取事务相关的属性 2)、再获取...
cglib动态代理 使用步骤: Step1:实现MethodInterceptor接口 Step2:创建方法对目标进行代理 Step3:重写intercept方法对代理目标方法进行拦截
nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor 就是少了这个包
本篇文章主要介绍了spring boot如何使用spring AOP实现拦截器,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
NULL 博文链接:https://88548886.iteye.com/blog/1528486
AOP联盟为Java的AOP提供了一系列标准接口,包括Advice通知及其继承接口MethodInterceptor方法拦截器,还有JointPoint连接点及其继承接口MethodInvocation。例如,很多具有AOP概念的框架如Spring AOP都会依赖于此来...
spring,aop的实现.MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor等。
* 代理类需要实现 MethodInterceptor(方法拦截器) * * 1.创建被代理类、创建代理类、代理类内部聚合被代理类 * 2.创建返回代理类实例方法 Enhancer.create(object.getClass(), this); * (1.创建工具类,2....
NULL 博文链接:https://chen106106.iteye.com/blog/1596135
@Around: 环绕增强,相当于MethodInterceptor. 这里Around的value参数应该写的是上面的签名,而不是runTime,如果@Around 参数值里 “@annotation (timeCost)" 是为了匹配 costTimeAround 方法的入参 timeCost,...
正如上面所示,在MethodInterceptor中你得自行决定是否调用MethodInvocation的proceed()方法来执行目标对象上的方法,proceed()方法在执行完后会返回目标对象上方法的执行结果。 MethodInterceptor在XML文件中的定义...
反射:动态代理设计模式 1.jdk 依赖接口 newProxyInstance方法 2.cglb 实现MethodInterceptor Aop 四种通知之前通
6.1.2拦截器及拦截器栈的应用 111 6.1.3自定义拦截器 115 6.2类型转换器 117 6.2.1struts2内置类型转换器 117 6.2.2引用类型的转换方式 117 6.2.3特殊对象的类型转换 118 6.2.4类型转换的错误处理 121 6.3...
java8 源码 所有项目demo都基于idea gradle + SpringBoot来开发构建 ...@Around:环绕增强,相当于MethodInterceptor @AfterReturning:后置增强,相当于AfterReturningAdvice,方法正常退出时执行 @Before
有一天在用回调模版的时候,忽然间又想到jdk自带的Proxy类似乎不是怎么好用,为何不把其做成回调模版呢,可以将其改造称类似spring Aop ...省去了去实现jdk的InvocationHandler,而且用户只关心对方法进行怎样的处理.
解决 Caused by: java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor
在上面这段代码中,重点注意三条命令的使用方法: assembly = Assembly.LoadFile(rootPath + prop.assemblyName); p = assembly.CreateInstance(prop.typeName); t.InvokeMember(prop.propertyName, BindingFlags....
在搭建spring项目时通常需要这些jar包 commons-logging-1.1.3.jar spring-aop-4.0.6.RELEASE.jar ... nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
实现MethodInterceptor简单的aop 分别用cglib和反射实现了一下