spring AOP 之 注解 配置实现(附 Java 代码实例)

转载自http://blog.csdn.net/qq_27093465/article/details/53381527


导入类扫描的注解解析器
命名空间:xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd"
引号添加这2个url
xml配置文件如下配置:<context:component-scan base-package="com.lxk.spring.aop.annotation"/>

导入springAOP的注解解析器
命名空间:xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
引号添加这2个url
xml配置文件如下配置:<aop:aspectj-autoproxy/>

上面的2个解析器的前后顺序不分先后。

接口类,目标接口

[java]  view plain  copy
  1. package com.lxk.spring.aop.annotation;  
  2.   
  3. import java.util.List;  
  4.   
  5. public interface PersonDao {  
  6.     void deletePerson();  
  7.     List<Person> getPerson() throws Exception;  
  8.     void savePerson();  
  9.     void updatePerson();  
  10. }  


接口实现类,目标对象
[java]  view plain  copy
  1. package com.lxk.spring.aop.annotation;  
  2.   
  3. import com.google.common.collect.Lists;  
  4. import org.springframework.stereotype.Repository;  
  5.   
  6. import java.util.List;  
  7.   
  8. /** 
  9.  * Created by lxk on 2016/11/28 
  10.  */  
  11. @Repository(value = "personDaoImpl")  
  12. public class PersonDaoImpl implements PersonDao {  
  13.   
  14.     public void deletePerson() {  
  15.         System.out.println("delete perosn");  
  16.     }  
  17.   
  18.     public List<Person> getPerson() throws Exception {  
  19.         List<Person> personList = Lists.newArrayList();  
  20.         Person person1 = new Person();  
  21.         person1.setPid(1L);  
  22.         person1.setPname("person1");  
  23.   
  24.         //int a = 1 / 0;  
  25.   
  26.         System.out.println("get person");  
  27.         personList.add(person1);  
  28.         Person person2 = new Person();  
  29.         person2.setPid(2L);  
  30.         person2.setPname("person2");  
  31.         personList.add(person2);  
  32.         return personList;  
  33.     }  
  34.   
  35.     public void savePerson() {  
  36.         System.out.println("delete perosn");  
  37.     }  
  38.   
  39.     public void updatePerson() {  
  40.         System.out.println("delete perosn");  
  41.     }  
  42.   
  43. }  


切面类
[java]  view plain  copy
  1. package com.lxk.spring.aop.annotation;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5. import org.aspectj.lang.annotation.*;  
  6. import org.springframework.stereotype.Component;  
  7.   
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. /** 
  12.  * 切面类 
  13.  * <p> 
  14.  * Created by lxk on 2016/11/28 
  15.  */  
  16. @Aspect  
  17. @Component(value = "transaction")  
  18. public class Transaction {  
  19.   
  20.     @Pointcut("execution(* com.lxk.spring.aop.annotation.PersonDaoImpl.*(..))")  
  21.     private void aa() {  
  22.     }//切入点签名  
  23.   
  24.     /** 
  25.      * 前置通知 
  26.      */  
  27.     @Before("aa()")  
  28.     public void beforeMethod() {  
  29.         System.out.println("before method");  
  30.     }  
  31.   
  32.     /** 
  33.      * 后置通知 
  34.      */  
  35.     @AfterReturning(value = "aa()", returning = "val")  
  36.     public void afterMethod(JoinPoint joinPoint, Object val) {  
  37.         List<Person> personList = (ArrayList<Person>) val;  
  38.         if (personList != null && !personList.isEmpty()) {  
  39.             for (Person person : personList) {  
  40.                 System.out.println(person.getPname());  
  41.             }  
  42.         }  
  43.         System.out.println("after method");  
  44.     }  
  45.   
  46.     /** 
  47.      * 最终通知 
  48.      */  
  49.     @After("aa()")  
  50.     public void finalMethod(JoinPoint joinPoint) {  
  51.         joinPoint.getArgs();  
  52.         System.out.println("final method");  
  53.     }  
  54.   
  55.     /** 
  56.      * 环绕通知 
  57.      */  
  58.     @Around("aa()")  
  59.     public Object aroundMethod(ProceedingJoinPoint joinPoint) {  
  60.         Object obj = null;  
  61.         try {  
  62.             obj = joinPoint.proceed();  
  63.         } catch (Throwable e) {  
  64.             System.out.println(e.getMessage());  
  65.         }  
  66.         System.out.println("around method");  
  67.         return obj;  
  68.     }  
  69.   
  70.     /** 
  71.      * 异常通知 
  72.      */  
  73.     @AfterThrowing(value = "aa()", throwing = "ex")  
  74.     public void throwingMethod(Throwable ex) {  
  75.         System.out.println(ex.getMessage());  
  76.     }  
  77. }  

xml配置文件

[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.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context  
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  11.     <aop:aspectj-autoproxy/>  
  12.     <context:component-scan base-package="com.lxk.spring.aop.annotation"/>  
  13. </beans>  

测试和model文件

[java]  view plain  copy
  1. package com.lxk.spring.aop.annotation;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. /** 
  8.  * Created by lxk on 2016/11/28 
  9.  */  
  10. public class AOPAnnotationTest {  
  11.     @Test  
  12.     public void test() throws Exception {  
  13.         ApplicationContext context = new ClassPathXmlApplicationContext("com/lxk/spring/aop/annotation/applicationContext.xml");  
  14.         PersonDao personDao = (PersonDao) context.getBean("personDaoImpl");  
  15.         personDao.getPerson();  
  16.         //List<Person> personList = personDao.getPerson("sss");  
  17.         //System.out.println(personList.size());  
  18.     }  
  19. }  

[java]  view plain  copy
  1. package com.lxk.spring.aop.annotation;  
  2.   
  3. /** 
  4.  * Created by lxk on 2016/11/28 
  5.  */  
  6. public class Person {  
  7.     private Long pid;  
  8.     private String pname;  
  9.   
  10.     public Long getPid() {  
  11.         return pid;  
  12.     }  
  13.   
  14.     public void setPid(Long pid) {  
  15.         this.pid = pid;  
  16.     }  
  17.   
  18.     public String getPname() {  
  19.         return pname;  
  20.     }  
  21.   
  22.     public void setPname(String pname) {  
  23.         this.pname = pname;  
  24.     }  
  25. }  

@Component(value="transaction")
解释:就是spring注解里面的注解,泛指,只是没有像@Service、@Controller 一样,具有特殊意义。
这个注解就是把切面类,纳入到spring容器管理,相当于。
@Component=bean id="transaction" class="..Transaction",这个写法。

@Aspect
此注解的作用:标识这个类就是切面。
@Aspect相当于xml文件中的aop配置标签:<aop:config> </aop:config>
然后切面里面,就有对应的切入点,通知等等。

@Pointcut 
此注解的作用:标注一个切入点
此注解不能单独存在,必须依附在切面
一个切入点声明有两个部分:一个包含名字和任意参数的签名,还有一个切入点表达式,该表达式决定了我们关注那个方法的执行。
作为切入点签名的方法必须返回void 类型
@Pointcut("execution(* com.lxk.spring.aop.annotation.PersonDaoImpl.*(..))")
private void aa(){}//切入点签名
相当于如下:在xml配置切入点。
<aop:pointcut expression="execution(* cn.itcast.spring0401.aop.annotation.PersonDaoImpl.*(..))" id="aa()"/>
然后现在切入点就是这个aa(),方法啦。


这个实现原理,spring容器有如下几次扫描过程
第一次,解析xml配置文件解析到类扫描的注解解析器,会在base-package指定的包及子包中扫描所有的类看类上是否有@Compontent,@Service,@Controller,@Repository注解,如果有,则spring容器创建该类的实例
第二次,解析到aop的注解解析器,会在纳入spring管理的bean中,看哪个类上面是否有@Aspect注解
第三次,在有@Aspect注解的类的所有方法中查找@Pointcut,就会找到切入点表达式,根据切入点表达式,在纳入spring范围的bean内查找,看哪个bean符合切入点表达式,如果符合则创建代理对象
当客户端访问某一个bean时,如果该bean有代理对象,则返回代理对象,否则返回该bean的对象


最后总结:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值