标签:contex advice color nat public red imp pat tde
目标对象接口
1 public interface IUserService { 2 public void add(); 3 public void update(); 4 public void del(); 5 public void search(); 6 }
1.创建目标对象
1 public class UserServiceImpl implements IUserService { 2 @Override 3 public void add() { 4 System.out.println("add...."); 5 } 6 @Override 7 public void update() { 8 System.out.println("update...."); 9 } 10 @Override 11 public void del() { 12 System.out.println("del...."); 13 } 14 @Override 15 public void search() { 16 System.out.println("search...."); 17 } 18 }
2.创建通知(增强 advice)
1 //adcice增强类 2 public class UserServiceHelper { 3 //前置通知 4 public void before(JoinPoint jp){ 5 System.out.println("拦截目标类"+jp.getSignature().getDeclaringTypeName()); 6 System.out.println("拦截目标方法名"+jp.getSignature().getName()); 7 System.out.println("前置通知"); 8 9 } 10 public void afterreturning(JoinPoint jp,Object val){ 11 System.out.println("目标返回值"+val); 12 System.out.println("后置通知"); 13 } 14 public Object around(ProceedingJoinPoint pjp) throws Throwable{ 15 System.out.println("环绕前通知"); 16 Object value = pjp.proceed(); 17 System.out.println("环绕后通知"); 18 return value; 19 } 20 public void afterthrowing(JoinPoint jp,Throwable ex){ 21 //有异常时才输出 22 System.out.println("异常抛出通知"+ex); 23 } 24 public void after(JoinPoint jp){ 25 System.out.println("拦截目标方法名"+jp.getSignature().getName()); 26 System.out.println("最终通知"); 27 } 28 }
3.1在Spring的applicationContext.xml配置:
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 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 7 <import resource="./aop4.xml" /> 8 </beans>
3.2在Spring的xmp配置apo1.xml
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:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 10 <!-- target 目标对象 --> 11 <bean id="userService" class="cn.aspectjs.UserServiceImpl"/> 12 <!-- advice 通知--> 13 <bean id="userServiceAdvice" class="cn.aspectjs.UserServiceHelper"/> 14 <!-- config是用来声明,内容表示不管有没有接口,都用aspectj框架实现aop,aspect用来配置切面 --> 15 <aop:config proxy-target-class="true"> 16 <aop:aspect ref="userServiceAdvice"> 17 <aop:pointcut expression="execution(* *.add(..))" id="MypointCut"/> 18 <!-- method是增强类 --> 19 <aop:before method=""/> 20 <aop:before method="before" pointcut-ref="MypointCut"/> 21 <aop:after-returning method="afterreturning" pointcut-ref="MypointCut" returning="val"/> 22 <aop:around method="around" pointcut-ref="MypointCut" /> 23 <aop:after-throwing method="afterthrowing" pointcut-ref="MypointCut" throwing="ex"/> 24 <aop:after method="after" pointcut-ref="MypointCut" /> 25 </aop:aspect> 26 </aop:config> 27 </beans>
4.测试类
//Spring整合JUnit测试
@RunWith(SpringJUnit4ClassRunner.class)
//参数定义了要装入的Spring配置文件 2 @ContextConfiguration(locations="classpath:applicationContext.xml") 3 public class AspectJTest{ 4 @Autowired 5 private IUserService userService; 6 @Test 7 public void test1(){ 8 //对应aop1.xml中的aspectj定义的通知. 9 userService.add(); 10 } 11 }
标签:contex advice color nat public red imp pat tde
原文地址:http://www.cnblogs.com/wwwzzz/p/7906128.html