java框架篇---spring aop两种配置方式

来源:互联网 发布:淘宝账号怎么找回来 编辑:程序博客网 时间:2024/05/19 23:05


  1.     public void add(User user) { 
  2.         userDAO.save(user); 
  3.     } 
  4.     public UserDAO getUserDAO() { 
  5.         return userDAO; 
  6.     } 
  7.      
  8.     @Resource(name="u"
  9.     public void setUserDAO( UserDAO userDAO) { 
  10.         this.userDAO = userDAO; 
  11.     } 
  12.     
  13.     public void destroy() { 
  14.         System.out.println("destroy"); 
  15.     } 

加入aop

  1. package com.bjsxt.aop; 
  2.  
  3. import org.aspectj.lang.annotation.After; 
  4. import org.aspectj.lang.annotation.AfterReturning; 
  5. import org.aspectj.lang.annotation.AfterThrowing; 
  6. import org.aspectj.lang.annotation.Aspect; 
  7. import org.aspectj.lang.annotation.Before; 
  8. import org.aspectj.lang.annotation.Pointcut; 
  9. import org.springframework.stereotype.Component; 
  10.  
  11. @Aspect 
  12. @Component 
  13. public class LogInterceptor { 
  14.     @Pointcut("execution(public * com.bjsxt.service..*.add(..))"
  15.     public void myMethod(){}; 
  16.      
  17.     /*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")*/ 
  18.     @Before("myMethod()"
  19.     public void before() { 
  20.         System.out.println("method staet"); 
  21.     }  
  22.     @After("myMethod()"
  23.     public void after() { 
  24.         System.out.println("method after"); 
  25.     }  
  26.     @AfterReturning("execution(public * com.bjsxt.dao..*.*(..))"
  27.     public void AfterReturning() { 
  28.         System.out.println("method AfterReturning"); 
  29.     }  
  30.     @AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))"
  31.     public void AfterThrowing() { 
  32.         System.out.println("method AfterThrowing"); 
  33.     }  

配置文件

  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             
  11.            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
  12.            "><!-- 要添加最后2行 --> 
  13.             
  14.     <context:annotation-config /> 
  15.     <context:component-scan base-package="com.bjsxt"/>  <!-- 自动扫描 --> 
  16.     <aop:aspectj-autoproxy/>  <!-- 要添加本行 --> 
  17. </beans> 

测试类:

  1. package com.bjsxt.service; 
  2. import org.junit.Test; 
  3. import org.springframework.context.ApplicationContext; 
  4. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  5.  
  6. import com.bjsxt.model.User; 
  7.  
  8. //Dependency Injection 
  9. //Inverse of Control 
  10. public class UserServiceTest { 
  11.  
  12.     @Test 
  13.     public void testAdd() throws Exception { 
  14.         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
  15.          
  16.          
  17.         UserService service = (UserService)ctx.getBean("userService"); 
  18.         System.out.println(service.getClass()); 
  19.         service.add(new User()); 
  20.         System.out.println("###"); 
  21.          
  22.         ctx.destroy(); 
  23.          
  24.     } 
  25.  

结果:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784
method staet
user save11d!
method AfterReturning
method after
###

注意:

@Aspect:意思是这个类为切面类 
@Componet:因为作为切面类需要 Spring 管理起来,所以在初始化时就需要将这个类初始化加入 Spring 的管理; 
@Befoe:切入点的逻辑(Advice)
execution…:切入点语法

第二种:xml配置aop

实例同上:只是配置文件不同

  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             
  11.            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
  12.            "><!-- 要添加最后2行 --> 
  13.             
  14.     <context:annotation-config /> 
  15.     <context:component-scan base-package="com.bjsxt"/> 
  16.     <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean> 
  17.     <aop:config> 
  18.         <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))"  
  19.         id="servicePointcut"/> 
  20.         <aop:aspect id="logAspect" ref="logInterceptor"
  21.             <aop:before method="before"  pointcut-ref="servicePointcut" /> 
  22.         </aop:aspect> 
  23.          
  24.     </aop:config> 
  25. </beans> 

下面的<beans>是Spring的配置标签,beans里面几个重要的属性:

xmlns:

是默认的xml文档解析格式,即spring的beans。地址是http://www.springframework.org/schema/beans。

通过设置这个属性,所有在beans里面声明的属性,可以直接通过<>来使用,比如<bean>等等。

xmlns:xsi:

是xml需要遵守的规范,通过URL可以看到,是w3的统一规范,后面通过xsi:schemaLocation来定位所有的解析文件。

xmlns:aop:

这个是重点,是我们这里需要使用到的一些语义规范,与面向切面AOP相关。

xmlns:tx:

Spring中与事务相关的配置内容。

一个XML文件,只能声明一个默认的语义解析的规范。

例如上面的xml中就只有beans一个是默认的,其他的都需要通过特定的标签来使用,比如aop,它自己有很多的属性,如果要使用,前面就必须加上aop:xxx才可以。比如上面的aop:config。

类似的,如果默认的xmlns配置的是aop相关的语义解析规范,那么在xml中就可以直接写config这种标签了。

  1.     public void add(User user) { 
  2.         userDAO.save(user); 
  3.     } 
  4.     public UserDAO getUserDAO() { 
  5.         return userDAO; 
  6.     } 
  7.      
  8.     @Resource(name="u"
  9.     public void setUserDAO( UserDAO userDAO) { 
  10.         this.userDAO = userDAO; 
  11.     } 
  12.     
  13.     public void destroy() { 
  14.         System.out.println("destroy"); 
  15.     } 

加入aop

  1. package com.bjsxt.aop; 
  2.  
  3. import org.aspectj.lang.annotation.After; 
  4. import org.aspectj.lang.annotation.AfterReturning; 
  5. import org.aspectj.lang.annotation.AfterThrowing; 
  6. import org.aspectj.lang.annotation.Aspect; 
  7. import org.aspectj.lang.annotation.Before; 
  8. import org.aspectj.lang.annotation.Pointcut; 
  9. import org.springframework.stereotype.Component; 
  10.  
  11. @Aspect 
  12. @Component 
  13. public class LogInterceptor { 
  14.     @Pointcut("execution(public * com.bjsxt.service..*.add(..))"
  15.     public void myMethod(){}; 
  16.      
  17.     /*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")*/ 
  18.     @Before("myMethod()"
  19.     public void before() { 
  20.         System.out.println("method staet"); 
  21.     }  
  22.     @After("myMethod()"
  23.     public void after() { 
  24.         System.out.println("method after"); 
  25.     }  
  26.     @AfterReturning("execution(public * com.bjsxt.dao..*.*(..))"
  27.     public void AfterReturning() { 
  28.         System.out.println("method AfterReturning"); 
  29.     }  
  30.     @AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))"
  31.     public void AfterThrowing() { 
  32.         System.out.println("method AfterThrowing"); 
  33.     }  

配置文件

  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             
  11.            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
  12.            "><!-- 要添加最后2行 --> 
  13.             
  14.     <context:annotation-config /> 
  15.     <context:component-scan base-package="com.bjsxt"/>  <!-- 自动扫描 --> 
  16.     <aop:aspectj-autoproxy/>  <!-- 要添加本行 --> 
  17. </beans> 

测试类:

  1. package com.bjsxt.service; 
  2. import org.junit.Test; 
  3. import org.springframework.context.ApplicationContext; 
  4. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  5.  
  6. import com.bjsxt.model.User; 
  7.  
  8. //Dependency Injection 
  9. //Inverse of Control 
  10. public class UserServiceTest { 
  11.  
  12.     @Test 
  13.     public void testAdd() throws Exception { 
  14.         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
  15.          
  16.          
  17.         UserService service = (UserService)ctx.getBean("userService"); 
  18.         System.out.println(service.getClass()); 
  19.         service.add(new User()); 
  20.         System.out.println("###"); 
  21.          
  22.         ctx.destroy(); 
  23.          
  24.     } 
  25.  

结果:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784
method staet
user save11d!
method AfterReturning
method after
###

注意:

@Aspect:意思是这个类为切面类 
@Componet:因为作为切面类需要 Spring 管理起来,所以在初始化时就需要将这个类初始化加入 Spring 的管理; 
@Befoe:切入点的逻辑(Advice)
execution…:切入点语法

第二种:xml配置aop

实例同上:只是配置文件不同

  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             
  11.            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
  12.            "><!-- 要添加最后2行 --> 
  13.             
  14.     <context:annotation-config /> 
  15.     <context:component-scan base-package="com.bjsxt"/> 
  16.     <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean> 
  17.     <aop:config> 
  18.         <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))"  
  19.         id="servicePointcut"/> 
  20.         <aop:aspect id="logAspect" ref="logInterceptor"
  21.             <aop:before method="before"  pointcut-ref="servicePointcut" /> 
  22.         </aop:aspect> 
  23.          
  24.     </aop:config> 
  25. </beans> 

下面的<beans>是Spring的配置标签,beans里面几个重要的属性:

xmlns:

是默认的xml文档解析格式,即spring的beans。地址是http://www.springframework.org/schema/beans。

通过设置这个属性,所有在beans里面声明的属性,可以直接通过<>来使用,比如<bean>等等。

xmlns:xsi:

是xml需要遵守的规范,通过URL可以看到,是w3的统一规范,后面通过xsi:schemaLocation来定位所有的解析文件。

xmlns:aop:

这个是重点,是我们这里需要使用到的一些语义规范,与面向切面AOP相关。

xmlns:tx:

Spring中与事务相关的配置内容。

一个XML文件,只能声明一个默认的语义解析的规范。

例如上面的xml中就只有beans一个是默认的,其他的都需要通过特定的标签来使用,比如aop,它自己有很多的属性,如果要使用,前面就必须加上aop:xxx才可以。比如上面的aop:config。

类似的,如果默认的xmlns配置的是aop相关的语义解析规范,那么在xml中就可以直接写config这种标签了。

0 0
原创粉丝点击