Spring AOP增强小例子

[java]  view plain  copy
  1. public interface ISomeService  {  
  2.     public  void  some();  
  3. }  

[java]  view plain  copy
  1. public class MyAdvice implements MethodBeforeAdvice{  
  2.     public void before(Method method, Object[] objects, Object o) throws Throwable {  
  3.         System.out.println("这是前置MethodBeforeAdvice");  
  4.     }  
  5. }  

[java]  view plain  copy
  1. public class MyAfter implements AfterReturningAdvice {  
  2.     public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {  
  3.         System.out.println("这是后置");  
  4.     }  
  5. }  

[java]  view plain  copy
  1. public class SomeService implements ISomeService{  
  2.     public  void  some(){  
  3.         System.out.println("这是测试 bean 代理");  
  4.     }  
  5. }  
[html]  view plain  copy
  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.   
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.   
  7.        xmlns:context="http://www.springframework.org/schema/context"  
  8.   
  9.        xmlns:p="http://www.springframework.org/schema/p"  
  10.   
  11.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  12.        http://www.springframework.org/schema/beans/spring-beans.xsd  
  13.   
  14.        http://www.springframework.org/schema/aop  
  15.        http://www.springframework.org/schema/aop/spring-aop.xsd  
  16.   
  17.        http://www.springframework.org/schema/context  
  18.        http://www.springframework.org/schema/context/spring-context.xsd  
  19.  ">  
  20.   
  21.     <!--属性-->  
  22.     <bean id="some" class="cn.springAop.SomeService"></bean>  
  23.   
  24.     <!--前置-->  
  25.     <bean id="advice" class="cn.springAop.MyAdvice"></bean>  
  26.   
  27.     <!--后置-->  
  28.     <bean id="after" class="cn.springAop.MyAfter"></bean>  
  29.   
  30.     <bean id ="DaiLi" class="org.springframework.aop.framework.ProxyFactoryBean">  
  31.         <!--需要增强的对象-->  
  32.         <property name="target" ref="some"></property>  
  33.         <!--需要拦截的方法-->  
  34.         <property name="interceptorNames" value="advice,after"></property>  
  35.         <!--加上下面 属性可以使 成为 CgLib 动态带来-->  
  36.         <property name="proxyTargetClass" value="true"></property>  
  37.   
  38.     </bean>  
  39. </beans>  

猜你喜欢

转载自blog.csdn.net/qq_36074218/article/details/76438624