注解方式AOP应用例子

8 篇文章 0 订阅

 

  1. Service接口: 
  2. public interface PersonService {  
  3.     public void save(String name);  
  4.     public void update(String name, Integer id);  
  5.     public String getName(Integer id);  
  6. }  
  7.    
  8. 接口实现:  
  9. public class PersonServiceimp implements PersonService{  
  10.    
  11.     @Override  
  12.     public void save(String name) {  
  13.        System.out.println("woshi save()");  
  14.     }  
  15.    
  16.     @Override  
  17.     public void update(String name, Integer id) {          
  18.        System.out.println("woshi update()");         
  19.     }  
  20.    
  21.     @Override  
  22.     public String getName(Integer id) {    
  23.        System.out.println("woshi getName()");  
  24.        return id+"";  
  25.     }  
  26.    
  27. }  
  28. 切面类:  
  29. @Aspect  
  30. public class Myintercepter {  
  31.      @Pointcut("execution (* snail.serviceimp.PersonServiceimp.*(..))")  
  32.      public void anyMethod(){}  
  33.      @Before("anyMethod() && args(userName)")//定义前置通知,anyMethod()是必须的方法,//args(username)不是必须的,是接收方法的参数  
  34.         public void doAccessCheck(String userName) {  
  35.               System.out.println("前置通知"+userName);  
  36.         }         
  37.         @AfterReturning(pointcut="anyMethod()",returning="revalue")//定义后置通知 returning参数是接收被拦截的方法的返回值  
  38.         public void doReturnCheck(String revalue) {  
  39.                   System.out.println("后置通知"+revalue);  
  40.         }  
  41.         @AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义例外通知  
  42.      public void doExceptionAction(Exception ex) {  
  43.                  System.out.println("例外通知"+ex);  
  44.         }  
  45.         @After("anyMethod()")//定义最终通知  
  46.         public void doReleaseAction() {  
  47.                  System.out.println("最终通知");  
  48.         }  
  49.         @Around("anyMethod()")//环绕通知  
  50.         public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {  
  51.                  System.out.println("环绕通知");  
  52.                  return pjp.proceed();  
  53.         }  
  54.    
  55. }  
 

 

 

 

 

xml配置文件

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.    
  3. <beans xmlns="http://www.springframework.org/schema/beans"   
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.        xmlns:context="http://www.springframework.org/schema/context"   
  6.        xmlns:aop="http://www.springframework.org/schema/aop"   
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans    
  8.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  9.          http://www.springframework.org/schema/context    
  10.           http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  11.            http://www.springframework.org/schema/aop    
  12.          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
  13.          ">  
  14.            <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
  15.             
  16.           
  17.            <bean id="aop" class="snail.service.Myintercepter"></bean>  
  18.            <bean id="pp" class="snail.serviceimp.PersonServiceimp"></bean>  
  19. </beans>  
 

 

 

 

测试类:

  1. public class test {            
  2.          public static void main(String[] args) {  
  3.                    ApplicationContextatx=new ClassPathXmlApplicationContext("applicationContext.xml");  
  4.                    PersonService b=(PersonService) atx.getBean("pp");  
  5.                    //Myintercepter ss=(Myintercepter)atx.getBean("aop");  
  6.                    b.save("保存snail");  
  7.                    b.getName(1);  
  8.                    b.update("gengxin"5);  
  9.          }  
  10.    
  11. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值