[转]Spring AOP的annotation实现

本文详细介绍了如何使用注解实现Spring AOP,包括导入相关jar包、配置applicationContext.xml、定义切面类及连接点类,并通过一个简单的例子展示了其应用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 记录一下使用注解实现spring AOP的小例子。

第一步,导入相关的jar包:aspectjweaver-1.6.8.jar(提供注解 org.aspectj.lang.annotation.Aspect等)、spring-aop-3.0.4.RELEASE.jar(提供自动代理 创建器 org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator)、 aopalliance-1.0.jar(提供拦截器功能)。

第二步,配置applicationContext.xml

  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns:jee="http://www.springframework.org/schema/jee"  xmlns:tx= "http://www.springframework.org/schema/tx"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  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-3.0.xsd    
  6.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  7.     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd    
  8.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  9.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">   
  10.     <!--其他配置在此省略...-->  
  11.     <!--配置aop自动创建代理-->  
  12.     <aop:aspectj-autoproxy/>  
  13.     <bean id="userManager"   class = "com.service.Impl.UserManagerImpl" ></bean>  
  14.     <bean class = "com.util.SecurityHandler" ></bean>  
  15. </beans>  

其中相关AOP的配置包括:

1、xmlns:aop="http://www.springframework.org/schema/aop "
2、 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" 其中XSD的版本可以在spring-aop.jar中查看

3、<aop:aspectj-autoproxy/>
4、<bean class="com.util.SecurityHandler"></bean>在spring容器中添加切面类bean

 

含有连接点的类UserManager

  1. package  com.service;  
  2.   
  3. import  com.entity.User;  
  4.   
  5. public   interface  UserManager {  
  6.     public   void  add(User user);  
  7. }  
  8.   
  9. package  com.service.Impl;  
  10.   
  11. import  org.apache.commons.logging.Log;  
  12. import  org.apache.commons.logging.LogFactory;  
  13.   
  14. import  com.entity.User;  
  15. import  com.service.UserManager;  
  16.   
  17. public   class  UserManagerImpl  implements  UserManager {  
  18.     private  Log log = LogFactory.getLog(UserManagerImpl. class );  
  19.   
  20.     public   void  add(User user) {  
  21.         log.debug("add User:"  + user.getUsername());  
  22.     }  
  23. }  

切面实现类SecurityHandler.java

  1. package  com.util;  
  2.   
  3. import  org.aspectj.lang.annotation.After;  
  4. import  org.aspectj.lang.annotation.Aspect;  
  5. import  org.aspectj.lang.annotation.Before;  
  6.   
  7. @Aspect   
  8. public   class  SecurityHandler {  
  9.     // 在执行指定方法前执行   
  10.     @Before ( "execution(* add*(..))" )  
  11.     private   void  checkSecurity() {  
  12.         System.out.println("添加前检查通过" );  
  13.     }  
  14.   
  15.     // 在执行指定方法后执行   
  16.     @After ( "execution(* add*(..))" )  
  17.     private   void  checkResult() {  
  18.         System.out.println("添加后检查通过" );  
  19.     }}  

测试方法:

  1. public   static   void  main(String[] args) {          
  2.         ApplicationContext context = new  ClassPathXmlApplicationContext( "applicationContext.xml" );  
  3.         UserManager userManager = (UserManager) context.getBean("userManager" );  
  4.   
  5.         User user = new  User();  
  6.         user.setUsername("aaa" );  
  7.         user.setPassword("bbb" );  
  8.   
  9.         userManager.add(user);  
  10.     }  

执行结果如下:

添加前检查通过
2011-03-11 16:13:21,562 [main] DEBUG [com.service.Impl.UserManagerImpl] - add User:aaa
添加后检查通过

 

总结:在搭建示例过程中遇到 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException异常, 提示无法获取spring-aop-3.0.xsd文件,后来发现是缺少spring-aop-3.0.4.RELEASE.jar,因为spring- aop-3.0.xsd在它里面。

 

 

 

来自:http://blog.youkuaiyun.com/cuihaiyang/article/details/6240413

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值