javaEE——Spring 四种切面技术(拦截)、获取Spring容器的两种办法

Spring AOP编程

     切面( Aspect ): 简单的理解就是 把那些与核心业务无关的代码提取出来,进行封装成一个或几个模块用来处理那些附加的功能代码 。(如日志,事务,安全验证)我们把这个模块的作用理解为一个切面,其实 切面 就是我们写 一个类 ,这个类中的代码原来是在业务模块中完成的,现在单独成一个或几个类。在业务模块需要的时候才织入。
    连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。 在Spring AOP中,一个连接点总是代表一个方法的执行。通过声明一个JoinPoint类型的参数可以使通知(Advice)的主体部分获得连接点信息。 
    切入点(Pointcut):本质上是一个捕获连接点的结构。在AOP中,可以定义一个pointcut,来捕获相关方法的调用
     织入(Weaving):把切面(aspect连接到其它的应用程序类型或者对象上,并创建一个被通知(advised)的对象。这些可以在编译时,类加载时和运行时完成。Spring和其它纯Java AOP框架一样,在运行时完成织入。
     通知(Advice):在切面的某个特定的连接点(Joinpoint)上执行的动作。通知有各种类型,其中包括“around”、“before”和“after”等通知。通知的类型将在后面部分进行讨论。许多AOP框架,包括Spring,都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。
通知的类型:
    前置通知(Before advice:在某连接点(join point)之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常)。
    返回后通知(After returning advice:在某连接点(join point)正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
    抛出异常后通知(After throwing advice:在方法抛出异常退出时执行的通知。
    后置通知(Afterfinallyadvice:当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

     环绕通知(Around Advice包围一个连接点join point)的通知,如方法调用。这是最强大的一种通知类型。 环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行



1.RegexpMethodPointcutAdvisor切面技术

下面演示代码:

纯Java方式写AOP(拦截技术)

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.aop;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import org.aopalliance.aop.Advice;  
  6. import org.aopalliance.intercept.MethodInterceptor;  
  7. import org.aopalliance.intercept.MethodInvocation;  
  8. import org.junit.Test;  
  9. import org.springframework.aop.Advisor;  
  10. import org.springframework.aop.AfterReturningAdvice;  
  11. import org.springframework.aop.MethodBeforeAdvice;  
  12. import org.springframework.aop.framework.ProxyFactory;  
  13. import org.springframework.aop.framework.ProxyFactoryBean;  
  14. import org.springframework.aop.support.DefaultPointcutAdvisor;  
  15. import org.springframework.aop.support.JdkRegexpMethodPointcut;  
  16.   
  17. import cn.hncu.spring4x.domain.Person;  
  18.   
  19. public class AopDemo {  
  20.     @Test//纯Java的方式实现切面(拦截)技术  
  21.     public void demo1(){  
  22.         Person p=new Person();  
  23.         ProxyFactory factory=new ProxyFactory(); //该类的功能没有ProxyFactoryBean强  
  24.         factory.setTarget(p);//1 给代理工厂一个原型对象  
  25.         //切面 = 切点 + 通知  
  26.         //切点  
  27.         JdkRegexpMethodPointcut pointcut=new JdkRegexpMethodPointcut();  
  28.         pointcut.setPattern("cn.hncu.spring4x.domain.Person.run");  
  29.         //      pointcut.setPattern(".*run.*");ProxyFactory对setPattern无效  
  30.         Advice advice=new MethodInterceptor() {  
  31.   
  32.             @Override  
  33.             public Object invoke(MethodInvocation invocation) throws Throwable {  
  34.                 System.out.println("前面拦截");  
  35.                 Object obj=invocation.proceed();  
  36.                 System.out.println("后面拦截");  
  37.                 return obj;  
  38.             }  
  39.         };  
  40.         //切面 = 切点 + 通知  
  41.         Advisor advisor=new DefaultPointcutAdvisor(pointcut, advice);  
  42.   
  43.         factory.addAdvice(advice);  
  44.   
  45.         Person p2=(Person) factory.getProxy();  
  46. //      p2.run();  
  47. //      p2.run(5);  
  48.         p2.say();  
  49.     }  
  50.     @Test//纯Java的方式实现切面(拦截)技术  
  51.     public void demo2(){  
  52.         ProxyFactoryBean factoryBean=new ProxyFactoryBean();  
  53.         factoryBean.setTarget(new Person());  
  54.         //切面 = 切点 + 通知  
  55.         //切点  
  56.         JdkRegexpMethodPointcut pointcut=new JdkRegexpMethodPointcut();  
  57.         pointcut.setPattern(".*run.*");  
  58.   
  59.         //通知  前切面---不需要放行,原方法也能执行  
  60.         Advice beforeAdvice=new MethodBeforeAdvice() {  
  61.             @Override  
  62.             public void before(Method method, Object[] args, Object target)  
  63.                     throws Throwable {  
  64.                 System.out.println("beforeAdvice拦截");//正则表达式有效  
  65.             }  
  66.         };  
  67.         Advice afterReturning=new AfterReturningAdvice() {  
  68.             @Override  
  69.             public void afterReturning(Object returnValue, Method method,  
  70.                     Object[] args, Object target) throws Throwable {  
  71.                 System.out.println("afterReturning");  
  72.             }  
  73.         };  
  74.   
  75.         Advice aroundAdvice=new MethodInterceptor() {  
  76.   
  77.             public Object invoke(MethodInvocation invocation) throws Throwable {  
  78.                 System.out.println("前面拦截");  
  79.                 Object obj=invocation.proceed();  
  80.                 System.out.println("后面拦截");  
  81.                 return obj;  
  82.             }  
  83.         };  
  84.         Advisor advisor1=new DefaultPointcutAdvisor(pointcut, beforeAdvice);  
  85.         Advisor advisor2=new DefaultPointcutAdvisor(pointcut, afterReturning);  
  86.         Advisor advisor3=new DefaultPointcutAdvisor(pointcut, aroundAdvice);  
  87.         factoryBean.addAdvisors(advisor1,advisor2,advisor3);  
  88.         //2 给代理工厂一个切面 ---注意,添加的顺序的拦截动作执行的顺序是有关系的!!!  
  89.         //先加的切面,如果拦前面,就拦在最前面,如果拦后面,就拦在最后面.  
  90.         Person p = (Person) factoryBean.getObject(); //3 从代理工厂中获取一个代理后的对象  
  91.         //p.run();  
  92.         //p.run(0);  
  93.         p.say();  
  94.     }  
  95.   
  96. }  

下面演示5种方式配置文件AOP


通知:AroundAdvice.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.aop;  
  2.   
  3. import org.aopalliance.intercept.MethodInterceptor;  
  4. import org.aopalliance.intercept.MethodInvocation;  
  5.   
  6. public class AroundAdvice implements MethodInterceptor {  
  7.   
  8.     @Override  
  9.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  10.         System.out.println("前面拦拦....");  
  11.         Object resObj = invocation.proceed();//放行  
  12.         System.out.println("后面拦拦.....");  
  13.         return resObj;  
  14.     }  
  15.   
  16. }  



测试代码,

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.aop;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class AopXmlDemo {  
  8.     @Test//采用配置文件的方式使用切面拦截  
  9.     public void demo1(){  
  10.         ApplicationContext act=new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/1.xml");  
  11.         Cat cat=act.getBean("catProxide",Cat.class);//要从catProxide返回  
  12.         cat.run();  
  13.         cat.say();  
  14.         cat.run(6);  
  15.     }  
  16.     @Test//把切点和通知配置成 切面的内部bean  
  17.     public void demo2(){  
  18.         ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/2.xml");  
  19.         Cat cat = ctx.getBean("catProxide",Cat.class);  
  20.         cat.run();  
  21.         cat.say();  
  22.         cat.run(7);  
  23.     }  
  24.     @Test//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置  
  25.     public void demo3(){  
  26.         ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/3.xml");  
  27.         Cat cat = ctx.getBean("catProxide",Cat.class);  
  28.         cat.run();  
  29.         cat.say();  
  30.         cat.run(7);  
  31.     }  
  32.     @Test//自动代理  
  33.     public void demo4(){  
  34.         ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/4.xml");  
  35.         Cat cat = ctx.getBean(Cat.class);  
  36.         cat.run();  
  37.         cat.say();  
  38.         cat.run(7);  
  39.     }  
  40.     @Test//自己写的自动代理  
  41.     public void demo5(){  
  42.         ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/5.xml");  
  43.         Cat cat = ctx.getBean("cat",Cat.class);  
  44. //      cat.run();  
  45. //      cat.say();  
  46. //      cat.run(7);  
  47.     }  
  48.       
  49. }  
1.xml
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  8.       
  9.     <bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>  
  10.     <!-- 切点 -->  
  11.     <bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">  
  12.         <property name="pattern" value=".*run.*"></property>  
  13.     </bean>  
  14.     <!-- 通知 ,要自己写-->  
  15.     <bean id="advice" class="cn.hncu.spring4x.aop.AroundAdvice"></bean>  
  16.       
  17.     <!-- 切面=切点+通知 -->  
  18.     <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">  
  19.         <property name="advice" ref="advice"></property>  
  20.         <property name="pointcut" ref="pointcut"></property>  
  21.     </bean>  
  22.       
  23.     <bean id="catProxide" class="org.springframework.aop.framework.ProxyFactoryBean">  
  24.         <property name="target" ref="cat"></property>  
  25.         <property name="interceptorNames">  
  26.             <list>  
  27.                 <value>advisor</value>  
  28.             </list>  
  29.         </property>  
  30.     </bean>  
  31. </beans>  

2.xml
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  8.       
  9.     <bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>  
  10.       
  11.     <!-- 切面=切点+通知 (把切点和通知写成内部bean)-->  
  12.     <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">  
  13.         <property name="advice">  
  14.             <bean class="cn.hncu.spring4x.aop.AroundAdvice"></bean>  
  15.         </property>  
  16.         <property name="pointcut">  
  17.             <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">  
  18.              <property name="patterns">  
  19.                 <list>  
  20.                     <value>.*run.*</value>  
  21.                     <value>.*say.*</value>  
  22.                 </list>  
  23.              </property>  
  24.                
  25.             </bean>  
  26.         </property>  
  27.     </bean>  
  28.       
  29.     <bean id="catProxide" class="org.springframework.aop.framework.ProxyFactoryBean">  
  30.         <property name="target" ref="cat"></property>  
  31.         <property name="interceptorNames">  
  32.             <list>  
  33.                 <value>advisor</value>  
  34.             </list>  
  35.         </property>  
  36.     </bean>  
  37. </beans>  


3.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  8.   
  9.     <bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>  
  10.   
  11.     <!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor -->  
  12.     <bean id="advisor"  
  13.         class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  14.         <property name="advice">  
  15.             <bean class="cn.hncu.spring4x.aop.AroundAdvice"></bean>  
  16.         </property>  
  17.         <property name="patterns">  
  18.             <list>  
  19.                 <value>.*run.*</value>  
  20.             </list>  
  21.         </property>  
  22.     </bean>  
  23.   
  24.     <bean id="catProxide" class="org.springframework.aop.framework.ProxyFactoryBean">  
  25.         <property name="target" ref="cat"></property>  
  26.         <property name="interceptorNames">  
  27.             <list>  
  28.                 <value>advisor</value>  
  29.             </list>  
  30.         </property>  
  31.     </bean>  
  32. </beans>  


4.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  8.   
  9.     <bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>  
  10.   
  11.     <!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor -->  
  12.     <bean id="advisor"  
  13.         class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  14.         <property name="advice">  
  15.             <bean class="cn.hncu.spring4x.aop.AroundAdvice"></bean>  
  16.         </property>  
  17.         <property name="patterns">  
  18.             <list>  
  19.                 <value>.*run.*</value>  
  20.             </list>  
  21.         </property>  
  22.     </bean>  
  23.     <!-- 自动代理 -->  
  24.     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>  
  25. </beans>  


5.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  8.   
  9.     <bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>  
  10.   
  11.     <!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor -->  
  12.     <bean id="advisor"  
  13.         class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  14.         <property name="advice">  
  15.             <bean class="cn.hncu.spring4x.aop.AroundAdvice"></bean>  
  16.         </property>  
  17.         <property name="patterns">  
  18.             <list>  
  19.                 <value>.*run.*</value>  
  20.             </list>  
  21.         </property>  
  22.     </bean>  
  23.     <!-- 自动代理 -->  
  24.       
  25.     <bean class="cn.hncu.spring4x.aop.MyAutoProxy"></bean>  
  26. </beans>  

第五种方法,模拟自动代理

MyAutoProxy.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.aop;  
  2.   
  3. import org.springframework.aop.Advisor;  
  4. import org.springframework.aop.framework.ProxyFactoryBean;  
  5. import org.springframework.beans.BeansException;  
  6. import org.springframework.beans.factory.config.BeanPostProcessor;  
  7. import org.springframework.context.ApplicationContext;  
  8. import org.springframework.context.ApplicationContextAware;  
  9.   
  10. public class MyAutoProxy implements BeanPostProcessor,ApplicationContextAware{  
  11.     private ApplicationContext applicationContext;  
  12.     @Override  
  13.     public void setApplicationContext(ApplicationContext applicationContext)  
  14.             throws BeansException {  
  15.         this.applicationContext=applicationContext;//保证是同一个容器  
  16.   
  17.     }  
  18.     @Override  
  19.     public Object postProcessBeforeInitialization(Object bean, String beanName)  
  20.             throws BeansException {  
  21.         System.out.println(bean+"postProcessBeforeInitialization");  
  22.         return bean; //直接放行(一定要)  
  23.     }  
  24.     @Override  
  25.     public Object postProcessAfterInitialization(Object bean, String beanName)  
  26.             throws BeansException {  
  27.         System.out.println(bean+"postProcessAfterInitialization");  
  28.         ProxyFactoryBean factoryBean=new ProxyFactoryBean();  
  29.         factoryBean.setTarget(bean);  
  30.         Advisor advisor=applicationContext.getBean("advisor", Advisor.class);  
  31.         factoryBean.addAdvisor(advisor);  
  32.         return factoryBean.getObject();  
  33.     }  
  34.   
  35.   
  36.       
  37. }  

2.AspectJExpressionPointcut切面技术

纯java代码演示:


Person.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.aspectj;  
  2.   
  3. public class Person {  
  4.     public void run(){  
  5.         System.out.println("run............");  
  6.     }  
  7.     public void run(int i){  
  8.         System.out.println(i+"run............");  
  9.     }  
  10.     public int run(String str,int i){  
  11.         System.out.println(str+"run............"+i);  
  12.         return 0;  
  13.     }  
  14.     public void run(String str){  
  15.         System.out.println(str+"run............");  
  16.     }  
  17.     public void say() {  
  18.         System.out.println("say............");  
  19.     }  
  20.     public Person say(String str) {  
  21.         System.out.println("say..........str.");  
  22.         return null;  
  23.     }  
  24. }  


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.aspectj;  
  2.   
  3. import org.aopalliance.aop.Advice;  
  4. import org.aopalliance.intercept.MethodInterceptor;  
  5. import org.aopalliance.intercept.MethodInvocation;  
  6. import org.junit.Test;  
  7. import org.springframework.aop.Advisor;  
  8. import org.springframework.aop.aspectj.AspectJExpressionPointcut;  
  9. import org.springframework.aop.framework.ProxyFactoryBean;  
  10. import org.springframework.aop.support.DefaultPointcutAdvisor;  
  11.   
  12. public class AspectjDemo {  
  13.   
  14.     @Test//纯java方式  
  15.     public void demo1(){  
  16.         ProxyFactoryBean factoryBean=new ProxyFactoryBean();  
  17.         factoryBean.setTarget(new Person());  
  18.           
  19.         //声明一个aspectj切点  
  20.         AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();  
  21.         //参数用切点语言来写  
  22. //      pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.run() )");//拦: 空参空返回值的run方法  
  23. //      pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*() )");//拦:  空参空返回值的任意方法  
  24. //      pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*(String) )"); //拦:  只有1个String类型参数,空返回值的任意方法  
  25. //      pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*(*) )"); //拦:  有1个参数(类型不限),空返回值的任意方法  
  26. //      pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*(..) )"); //拦:  任意(个数和类型)参数,空返回值的任意方法  
  27. //      pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*(*,..) )"); //拦:  至少有1个参数(类型不限),空返回值的任意方法  
  28.           
  29. //      pointcut.setExpression("execution( * cn.hncu.spring4x.aspectj.Person.*(*,*) )"); //拦:  有2个参数(类型不限),任意返回值的任意方法  
  30.         pointcut.setExpression("execution( cn.hncu.spring4x.aspectj.Person cn.hncu.spring4x.aspectj.Person.*(*,..) )"); //拦:  至少有1个参数(类型不限),返回值类型是Person的任意方法——不是基础数据类型要用全名  
  31.         pointcut.setExpression("execution( * cn.hncu..**son.*(..) )"); //拦: cn.hncu包下,类名以"son"结束,   函数、返回类型和参数任意  
  32.         Advice advice=new MethodInterceptor() {  
  33.             @Override  
  34.             public Object invoke(MethodInvocation invocation) throws Throwable {  
  35.                 System.out.println("前面拦拦");  
  36.                 Object obj=invocation.proceed();  
  37.                 System.out.println("后面拦拦");  
  38.                 return obj;  
  39.             }  
  40.         };  
  41.         //切面=切点+通知  
  42.         Advisor advisor=new DefaultPointcutAdvisor(pointcut, advice);  
  43.         factoryBean.addAdvisor(advisor);  
  44.         Person p=(Person) factoryBean.getObject();  
  45.         p.run();  
  46.         p.run(5);  
  47.         p.run("有返回值有int0",5);  
  48.         p.run("没有返回值");  
  49.         p.say();  
  50.     }  
  51. }  

xml配置文件演示

测试代码

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.aspectj;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class AspectjXmlDemo {  
  8.     @Test  
  9.     public void demo(){  
  10.         ApplicationContext act=new ClassPathXmlApplicationContext("cn/hncu/spring4x/aspectj/aspectj.xml");  
  11.         Person p=act.getBean("person", Person.class);  
  12.         p.run();  
  13.         p.run(5);  
  14.         p.run("有返回值有int0",5);  
  15.         p.run("没有返回值");  
  16.         p.say();  
  17.     }  
  18.     @Test  
  19.     public void demo2(){  
  20.         ApplicationContext act=new ClassPathXmlApplicationContext("cn/hncu/spring4x/aspectj/aspectj2.xml");  
  21.         Person p=act.getBean("person", Person.class);  
  22.         p.run();  
  23.         p.run(5);  
  24.         p.run("有返回值有int0",5);  
  25.         p.run("没有返回值");  
  26.         p.say();  
  27.     }  
  28. }  
AroundAdvice通知

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.aspectj;  
  2.   
  3. import org.aopalliance.intercept.MethodInterceptor;  
  4. import org.aopalliance.intercept.MethodInvocation;  
  5.   
  6. public class AroundAdvice implements MethodInterceptor {  
  7.   
  8.     @Override  
  9.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  10.         System.out.println("前面拦拦....");  
  11.         Object resObj = invocation.proceed();//放行  
  12.         System.out.println("后面拦拦.....");  
  13.         return resObj;  
  14.     }  
  15.   
  16. }  
aspectj.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  8.     <bean id="person" class="cn.hncu.spring4x.aspectj.Person"></bean>  
  9.     <bean id="advice" class="cn.hncu.spring4x.aspectj.AroundAdvice"></bean>  
  10.     <bean id="pointcut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">  
  11.         <property name="expression" value="execution( * cn.hncu..*son.*(*,..) )"></property>  
  12.     </bean>  
  13.     <!-- 切面=切点+通知 (※※采用面向切点语言进行配置切面)-->  
  14.     <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">  
  15.         <property name="advice" ref="advice"></property>  
  16.         <property name="pointcut" ref="pointcut"></property>  
  17.     </bean>  
  18.     <!-- 自动代理 -->  
  19.     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>  
  20. </beans>  
aspectj2.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  8.     <bean id="person" class="cn.hncu.spring4x.aspectj.Person"></bean>  
  9.     <!-- 切面=切点+通知 (※※采用面向切点语言进行配置切面)org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor -->  
  10.     <bean id="advisor"  
  11.         class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">  
  12.         <property name="expression" value="execution( * cn.hncu..*son.*(*,..) )"></property>  
  13.         <property name="advice">  
  14.             <bean id="advice" class="cn.hncu.spring4x.aspectj.AroundAdvice"></bean>  
  15.         </property>  
  16.     </bean>  
  17.     <!-- 自动代理 -->  
  18.     <bean  
  19.         class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>  
  20. </beans>  


实战:通过aspectj对c3po中多的的connection对象的close()方法进行拦截,保证同一用户拿到同一线程,以及实现事务


这是applicationContext.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:context="http://www.springframework.org/schema/context"  
  5.         xmlns:tx="http://www.springframework.org/schema/tx"  
  6.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  9.     <context:property-placeholder location="WEB-INF/conf/jdbc.properties"/>  
  10.     <bean id="dataSourse" class="org.springframework.jdbc.datasource.SimpleDriverDataSource" >  
  11.         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
  12.         <property name="url" value="${url}"></property>  
  13.         <property name="username" value="${name}"></property>  
  14.         <property name="password" value="${pwd}"></property>  
  15.         <!-- 不能用${username}和${password} -->  
  16.     </bean>  
  17.       
  18.     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>  
  19.     <bean id="tx"  
  20.         class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">  
  21.         <property name="expression" value="execution( * cn.hncu..*Service.*(..) )"></property>  
  22.         <!-- 要拦Service不能拦ServiceImpl依赖抽象  -->  
  23.         <property name="advice">  
  24.             <bean class="cn.hncu.utils.TxAdvice"></bean>  
  25.         </property>  
  26.         </bean>  
  27.         <bean id="closeCon" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">  
  28.             <property name="expression" value="execution( * *..*.*.getConnection() )"></property>  
  29.             <property name="advice">  
  30.             <bean class="cn.hncu.utils.CloseAdvice"></bean>  
  31.         </property>  
  32.         </bean>  
  33. </beans>  

TxAdvice.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.utils;  
  2.   
  3. import java.sql.Connection;  
  4.   
  5. import javax.sql.DataSource;  
  6.   
  7. import org.aopalliance.intercept.MethodInterceptor;  
  8. import org.aopalliance.intercept.MethodInvocation;  
  9. import org.springframework.beans.BeansException;  
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.ApplicationContextAware;  
  12.   
  13.   
  14. public class TxAdvice implements MethodInterceptor,ApplicationContextAware{  
  15.     private ApplicationContext act;  
  16.     @Override  
  17.     public void setApplicationContext(ApplicationContext act)  
  18.             throws BeansException {  
  19.         this.act=act;  
  20.   
  21.     }  
  22.     @Override  
  23.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  24.         DataSource ds=act.getBean("dataSourse", DataSource.class);  
  25.         Connection con=ds.getConnection();  
  26.         con.setAutoCommit(false);  
  27.         System.out.println("开启一个事务");  
  28.         Object obj = null;  
  29.         try {  
  30.             obj = invocation.proceed();  
  31.             System.out.println("提交一个事务");  
  32.             con.commit();  
  33.         } catch (Exception e) {  
  34.             System.out.println("回滚一个事务");  
  35.             con.rollback();  
  36.         }finally{  
  37.             try {  
  38.                 con.setAutoCommit(true);  
  39.                 con.close();  
  40.             } catch (Exception e2) {  
  41.             }  
  42.         }  
  43.         return obj;  
  44.     }  
  45.   
  46.   
  47. }  

CloseAdive.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.utils;  
  2.   
  3. import java.lang.reflect.Method;  
  4. import java.sql.Connection;  
  5.   
  6. import net.sf.cglib.proxy.Callback;  
  7. import net.sf.cglib.proxy.Enhancer;  
  8. import net.sf.cglib.proxy.MethodProxy;  
  9.   
  10. import org.aopalliance.intercept.MethodInterceptor;  
  11. import org.aopalliance.intercept.MethodInvocation;  
  12.   
  13. public class CloseAdvice implements MethodInterceptor{//代理getConnetion  
  14.     private ThreadLocal<Connection> tl=new ThreadLocal<Connection>();  
  15.     @Override  
  16.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  17.         Connection con=tl.get();  
  18.         if(con!=null){  
  19.             System.out.println(con.hashCode()+":::::"+invocation.getMethod().getName());  
  20.         }  
  21.         if(con==null){  
  22.             final Connection con2=(Connection) invocation.proceed();  
  23.               
  24.             Callback callback = new net.sf.cglib.proxy.MethodInterceptor(){  
  25.                 @Override  
  26.                 public Object intercept(Object proxiedObj, Method method,  
  27.                         Object[] args, MethodProxy proxy) throws Throwable {  
  28.                     if(method.getName().equalsIgnoreCase("close")){  
  29.                         System.out.println("close代理"+proxiedObj);  
  30.                         return null;  
  31.                     }  
  32.                     return method.invoke(con2, args);  
  33.                 }  
  34.             };  
  35.             con=(Connection) Enhancer.create(Connection.class, callback);  
  36.             tl.set(con);  
  37.         }  
  38.   
  39.         return con;  
  40.     }  
  41.   
  42. }  
在这里测试出c3p0每次增删改都会调用一次close方法

3.注解(POJO)-----aop自动代理    xmlns:aop="http://www.springframework.org/schema/aop"

注意:要在对于不同的jdk版本要加入不同的织入包,因为我的是jdk1.7所有我加入的包是1.7版本的

通过注解来对进行切面

anno.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
  8.                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">  
  9.   
  10.   
  11.     <!-- 使用aop标签配自动代理 -->  
  12.     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
  13.       
  14.     <bean id="p" class="cn.hncu.spring4x.annoAop.Person"></bean>  
  15.   
  16.     <!--基于注解的切面(切面=切点+通知),该类通过@Aspect注解让自动代理知道它是一个切面 -->  
  17.     <!-- 切点寄宿在方法上 -->  
  18.     <!-- <bean class="cn.hncu.spring4x.annoAop.MyAdivor"></bean> -->  
  19.     <!-- 切点寄宿在属性上 -->  
  20.     <bean class="cn.hncu.spring4x.annoAop.MyAdivor2"></bean>  
  21.   
  22. </beans>  

Myadvisor.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.annoAop;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.After;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6. import org.aspectj.lang.annotation.AfterThrowing;  
  7. import org.aspectj.lang.annotation.Around;  
  8. import org.aspectj.lang.annotation.Aspect;  
  9. import org.aspectj.lang.annotation.Before;  
  10. import org.aspectj.lang.annotation.Pointcut;  
  11. @Aspect //1 把当前类标记为一个切面--否则自动代理不知道该类是一个切面  
  12. public class MyAdivor {  
  13.     //2 切点  ---通过方法名标识该切点  
  14.     @Pointcut(value="execution( * cn.hncu..*son.*(*,..) )")  
  15.     public void pointcut(){  
  16.     }  
  17.       
  18.     @Before(value="pointcut()")//3 指定通知类型为before,切点为px()方法上的那个@Pointcut注解  
  19.     public void before(){  
  20.         System.out.println("在拦截之前");  
  21.     }  
  22.       
  23.     @After(value="pointcut()")  
  24.     public void after(){  
  25.         System.out.println("在拦截之后");  
  26.     }  
  27.     @Around(value="pointcut()")//Spring不建议使用周围通知,因为要依赖于ProceedingJoinPoint类---建议联合使用“@Before”和“@After”来实现周围通知的功能  
  28.     public Object  around(ProceedingJoinPoint pjp) throws Throwable{  
  29.         System.out.println("前");  
  30.         Object res = pjp.proceed();  
  31.         System.out.println("后");  
  32.         return res;  
  33.     }  
  34.     @AfterReturning(value="pointcut()")//理解“正常返回”: 该通知在方法出现异常又没有捕捉时,是不会执行(没有出现异常)  
  35.     public void afternReturning(){  
  36.         System.out.println("没有出现异常");  
  37.     }  
  38.     @AfterThrowing(value="pointcut()")//如果被拦截方法出现没捕捉的异常,则该方法会执行。反之,不会执行...(有异常)  
  39.     public void afternThrowing(){  
  40.         System.out.println("被拦截方法出现没捕捉的异常");  
  41.     }  
  42. }  

Myadvisor.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.annoAop;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.After;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6. import org.aspectj.lang.annotation.AfterThrowing;  
  7. import org.aspectj.lang.annotation.Around;  
  8. import org.aspectj.lang.annotation.Aspect;  
  9. import org.aspectj.lang.annotation.Before;  
  10. import org.aspectj.lang.annotation.Pointcut;  
  11. @Aspect //1 把当前类标记为一个切面--否则自动代理不知道该类是一个切面  
  12. public class MyAdivor2 {  
  13.     //2 切点(基于属性的切点)  ---通过属性名标识该切点  
  14.     private final String CUT="execution( * cn.hncu..*son.*(*,..))";  
  15.     //2 切点  ---通过方法名标识该切点  
  16.     @Pointcut(value=CUT)  
  17.     public void pointcut(){  
  18.     }  
  19.       
  20.     @Before(value=CUT)//3 指定通知类型为before,切点为px()方法上的那个@Pointcut注解  
  21.     public void before(){  
  22.         System.out.println("在拦截之前");  
  23.     }  
  24.       
  25.     @After(value=CUT)  
  26.     public void after(){  
  27.         System.out.println("在拦截之后");  
  28.     }  
  29.     @Around(value=CUT)//Spring不建议使用周围通知,因为要依赖于ProceedingJoinPoint类---建议联合使用“@Before”和“@After”来实现周围通知的功能  
  30.     public Object  around(ProceedingJoinPoint pjp) throws Throwable{  
  31.         System.out.println("前");  
  32.         Object res = pjp.proceed();  
  33.         System.out.println("后");  
  34.         return res;  
  35.     }  
  36.     @AfterReturning(value=CUT)//理解“正常返回”: 该通知在方法出现异常又没有捕捉时,是不会执行(没有出现异常)  
  37.     public void afternReturning(){  
  38.         System.out.println("没有出现异常");  
  39.     }  
  40.     @AfterThrowing(value=CUT)//如果被拦截方法出现没捕捉的异常,则该方法会执行。反之,不会执行...(有异常)  
  41.     public void afternThrowing(){  
  42.         System.out.println("被拦截方法出现没捕捉的异常");  
  43.     }  
  44. }  



在Web项目中使用注解处理事务:(JdbcDaoSupport,org.springframework.jdbc.datasource.DataSourceTransactionManager,@Transactional)

applicationContext.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
  8.                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">  
  9.   
  10.     <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  11.          <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
  12.          <property name="url" value="jdbc:mysql:///sstud?characterEncoding=UTF-8"></property>  
  13.          <property name="username" value="root"></property>  
  14.          <property name="password" value="1234"></property>  
  15.     </bean>  
  16.   
  17.     <bean id="closeCon"  
  18.         class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">  
  19.         <property name="expression" value="execution( * *..*.*.getConnection() )"></property>  
  20.         <property name="advice">  
  21.             <bean class="cn.hncu.utils.CloseAdvice"></bean>  
  22.         </property>  
  23.     </bean>  
  24.     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
  25.     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  26.         <property name="dataSource" ref="dataSource"></property>  
  27.     </bean>  
  28.     <tx:annotation-driven proxy-target-class="true" transaction-manager="txManager"/>  
  29.           
  30. </beans>  

StudDaoJdbc.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.stud.dao;  
  2.   
  3. import java.sql.SQLException;  
  4. import java.util.UUID;  
  5.   
  6. import javax.sql.DataSource;  
  7.   
  8. import org.apache.commons.dbutils.QueryRunner;  
  9. import org.springframework.jdbc.core.support.JdbcDaoSupport;  
  10.   
  11. import cn.hncu.stud.domain.Book;  
  12. import cn.hncu.stud.domain.Stud;  
  13.   
  14. public class StudDaoImpl extends JdbcDaoSupport implements StudDao {  
  15.     @Override  
  16.     public void saveStud(Stud stud) throws SQLException {  
  17.         stud.setId(UUID.randomUUID().toString().replaceAll("-",  "").substring(0,5));  
  18.         getJdbcTemplate().update("insert stud(id,name) value(?,?)",stud.getId(),stud.getName());  
  19.     }  
  20.   
  21.     @Override  
  22.     public void saveBook(Book book) throws SQLException {  
  23.         getJdbcTemplate().update("insert book(name) value(?)",book.getName());  
  24.           
  25.     }  
  26.   
  27. }  

StudServiceImpl.java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.stud.service;  
  2.   
  3. import java.sql.SQLException;  
  4.   
  5. import javax.ejb.TransactionManagement;  
  6.   
  7. import org.springframework.transaction.annotation.Propagation;  
  8. import org.springframework.transaction.annotation.Transactional;  
  9.   
  10.   
  11. import cn.hncu.stud.dao.StudDao;  
  12. import cn.hncu.stud.domain.Book;  
  13. import cn.hncu.stud.domain.Stud;  
  14.   
  15. public class StudServiceImpl implements IStudService{  
  16.     private StudDao dao=null;  
  17.     @Override  
  18.     @Transactional(propagation=Propagation.REQUIRES_NEW)  
  19.     public void save(Stud stud, Book book) throws SQLException {  
  20.         dao.saveStud(stud);  
  21.         dao.saveBook(book);  
  22.           
  23.     }  
  24.     public StudDao getDao() {  
  25.         return dao;  
  26.     }  
  27.     public void setDao(StudDao dao) {  
  28.         this.dao = dao;  
  29.     }  
  30.   
  31. }  




4.纯POJO切面技术(使用<aop:config>来进行配置)

pojo.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
  8.                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">  
  9.   
  10.   
  11.     <!-- 使用aop标签配自动代理 -->  
  12.     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
  13.       
  14.     <bean id="p" class="cn.hncu.spring4x.annoAop.Person"></bean>  
  15.     <!-- 基于POJO的切面 -->  
  16.     <bean id="pojo" class="cn.hncu.spring4x.pojoAop.MyAdivsor"></bean>  
  17.     <aop:config>  
  18.         <aop:pointcut expression="execution( * cn.hncu..*son.*(*,..) )" id="pointcut"/>  
  19.         <aop:aspect ref="pojo">  
  20.             <aop:after method="after" pointcut-ref="pointcut"/>  
  21.             <aop:before method="before" pointcut-ref="pointcut"/>  
  22.         </aop:aspect>  
  23.     </aop:config>  
  24. </beans>  

MyAdvisor,java

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.hncu.spring4x.pojoAop;  
  2.   
  3. public class MyAdivsor {  
  4.     public void before(){  
  5.         System.out.println("之前拦拦.....");  
  6.     }  
  7.     public void after(){  
  8.         System.out.println("之后拦拦666....");  
  9.     }  
  10. }  









获得获取Spring容器的两种办法(ApplicationContext)

1.在Servlet中通过WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());去拿

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.        @Override//※※※※※获取Web中的spring容器---法1  
  2. public void init() throws ServletException {  
  3.     ApplicationContext act=WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());  
  4.     service=act.getBean("studService",IStudService.class);  
  5. }  


2.通过实现ApplicationContextAware去拿


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">public class TxAdvice implements ApplicationContextAware{  
  2.     private ApplicationContext act;  
  3.     @Override  
  4.     public void setApplicationContext(ApplicationContext act)  
  5.             throws BeansException {  
  6.         this.act=act;  
  7.   
  8.     }  
  9. }</span>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值