Java Spring AOP的两种配置方式

来源:互联网 发布:求二端口网络z参数 编辑:程序博客网 时间:2024/05/20 00:36
第一种:注解配置AOP

java中注解配置AOP(使用 AspectJ 类库实现的),大致分为三步:

1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2. 开发需要被拦截的类。
3. 将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式。这样的话,那就交由Spring AoP容器管理。

另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar

实例:

  1. User.java
  2. package com.bjsxt.model;

  3. public class User {
  4.     private String username;
  5.     private String password;
  6.     public String getUsername() {
  7.         return username;
  8.     }
  9.     public void setUsername(String username) {
  10.         this.username = username;
  11.     }
  12.     public String getPassword() {
  13.         return password;
  14.     }
  15.     public void setPassword(String password) {
  16.         this.password = password;
  17.     }
  18. }
  19. /**
  20. *接口类
  21. */
  22. package com.bjsxt.dao;
  23. import com.bjsxt.model.User;

  24. public interface UserDAO {
  25.     public void save(User user);
  26. }
复制代码
实现接口:
  1. package com.bjsxt.dao.impl;

  2. import org.springframework.stereotype.Component;

  3. import com.bjsxt.dao.UserDAO;
  4. import com.bjsxt.model.User;

  5. @Component("u")
  6. public class UserDAOImpl implements UserDAO {

  7.     public void save(User user) {

  8.         System.out.println("user save11d!");
  9.         /*throw new RuntimeException("exception");*/ //抛异常
  10.     }

  11. }
复制代码
操作类:
  1. package com.bjsxt.service;
  2. import javax.annotation.Resource;

  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.stereotype.Component;

  6. import com.bjsxt.dao.UserDAO;
  7. import com.bjsxt.model.User;

  8. @Component("userService")
  9. public class UserService {

  10.     private UserDAO userDAO;  

  11.     public void init() {
  12.         System.out.println("init");
  13.     }

  14.     public void add(User user) {
  15.         userDAO.save(user);
  16.     }
  17.     public UserDAO getUserDAO() {
  18.         return userDAO;
  19.     }

  20.     @Resource(name="u")
  21.     public void setUserDAO( UserDAO userDAO) {
  22.         this.userDAO = userDAO;
  23.     }

  24.     public void destroy() {
  25.         System.out.println("destroy");
  26.     }
  27. }
复制代码
加入aop
  1. package com.bjsxt.aop;

  2. import org.aspectj.lang.annotation.After;
  3. import org.aspectj.lang.annotation.AfterReturning;
  4. import org.aspectj.lang.annotation.AfterThrowing;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Before;
  7. import org.aspectj.lang.annotation.Pointcut;
  8. import org.springframework.stereotype.Component;

  9. @Aspect
  10. @Component
  11. public class LogInterceptor {
  12.     @Pointcut("execution(public * com.bjsxt.service..*.add(..))")
  13.     public void myMethod(){};

  14.     /*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")*/
  15.     @Before("myMethod()")
  16.     public void before() {
  17.         System.out.println("method staet");
  18.     } 
  19.     @After("myMethod()")
  20.     public void after() {
  21.         System.out.println("method after");
  22.     } 
  23.     @AfterReturning("execution(public * com.bjsxt.dao..*.*(..))")
  24.     public void AfterReturning() {
  25.         System.out.println("method AfterReturning");
  26.     } 
  27.     @AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))")
  28.     public void AfterThrowing() {
  29.         System.out.println("method AfterThrowing");
  30.     } 
  31. }
复制代码
配置文件
  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.     <context:annotation-config />
  14.     <context:component-scan base-package="com.bjsxt"/>  <!-- 自动扫描 -->
  15.     <aop:aspectj-autoproxy/>  <!-- 要添加本行 -->
  16. </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. import com.bjsxt.model.User;

  6. //Dependency Injection
  7. //Inverse of Control
  8. public class UserServiceTest {

  9.     @Test
  10.     public void testAdd() throws Exception {
  11.         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

  12.         UserService service = (UserService)ctx.getBean("userService");
  13.         System.out.println(service.getClass());
  14.         service.add(new User());
  15.         System.out.println("###");

  16.         ctx.destroy();

  17.     }

  18. }
复制代码
结果:
  1. class com.bjsxt.service.UserService$EnhancerByCGLIB$7b201784
  2. method staet
  3. user save11d!
  4. method AfterReturning
  5. method after
  6. ###
复制代码

注意:

  • @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.     <context:annotation-config />
  14.     <context:component-scan base-package="com.bjsxt"/>
  15.     <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
  16.     <aop:config>
  17.         <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))" 
  18.         id="servicePointcut"/>
  19.         <aop:aspect id="logAspect" ref="logInterceptor">
  20.             <aop:before method="before"  pointcut-ref="servicePointcut" />
  21.         </aop:aspect>

  22.     </aop:config>
  23. </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
原创粉丝点击