注意: 下面的配置不是拦截Controller的, 拦截Controller的需要在servlet.xml, 在扫描Controller的后面加入, 如果是拦截service,那么应该放在扫描service注解的
context.xml:
1 2 3 4 5 |
<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> |
其中拦截器代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
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两种方式配置,如下所示:
1 2 3 4 5 6 |
<aop:config> <aop:aspectrefaop:aspectrefaop:aspectrefaop:aspectref="aspectDef"> <aop:pointcutidaop:pointcutidaop:pointcutidaop:pointcutid="pointcut1"expression="execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))"/> <aop:before pointcut-ref="pointcut1" method="beforeAdvice" /> </aop:aspect> </aop:config> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@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..."); } |
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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
1 2 3 4 5 6 7 8 9 |
<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> |
测试例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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