Spring学习笔记(三)
AOP
一、使用Annotation方式实现AOP。步骤:
- xml里加入配置:<aop:aspectj-autoproxy />
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:annotation-config /> <context:component-scan base-package="com.wangj.spring" /> <aop:aspectj-autoproxy /> </beans>
- 再引入如下jar包:(注意:必须引入第三个jar)
- @Aspect注解这个类
- @Before @After等来注解方法
- 写明切入点(execution(...))
- 将拦截器类交给Spring管理(@Component注解这个类)
@Aspect @Component public class LogInterceptor { @Before("execution(public void com.wangj.spring.dao.impl.UserDaoImpl.save(com.wangj.spring.model.User))") public void before() { System.out.println("method before"); } @After("execution(public void com.wangj.spring.dao.impl.UserDaoImpl.save(com.wangj.spring.model.User))") public void after() { System.out.println("method after(finally)"); } }
二、织入点语法
execution(public * *(..)) // 作用于任何类任何方法
execution(* set*(..)) // 作用于任何以set开头的方法
execution(* com.wangj.spring.dao.impl.UserDaoImpl.*(..)) //作用于UserDaoImpl类的所有方法
execution(* com.wangj.spring.dao..*.*(..)) //作用于com.wangj.spring.dao包(包括所有子包)里的所有类的所有方法
execution(public void com.wangj.spring.dao.impl.UserDaoImpl.save(com.wangj.spring.model.User)) // 作用于UserDaoImpl类的save(User)方法
三、Annotation详解
1. @Pointcut 织入点集合
@Aspect @Component public class LogInterceptor { @Pointcut("execution(* com.wangj.spring.dao..*.*(..))") public void pointcutMethod() {} // @Before("execution(public void com.wangj.spring.dao.impl.UserDaoImpl.save(com.wangj.spring.model.User))") @Before("pointcutMethod()") public void before() { System.out.println("method before"); } // @After("execution(public void com.wangj.spring.dao.impl.UserDaoImpl.save(com.wangj.spring.model.User))") @After("pointcutMethod()") public void after() { System.out.println("method after(finally)"); } }
2. @Before @After @AfterReturning @AfterThrowing @Around
@Aspect @Component public class LogInterceptor { @Pointcut("execution(* com.wangj.spring.dao..*.*(..))") public void pointcutMethod() {} // @Before("execution(public void com.wangj.spring.dao.impl.UserDaoImpl.save(com.wangj.spring.model.User))") @Before("pointcutMethod()") public void before() { System.out.println("method before"); } // @After("execution(public void com.wangj.spring.dao.impl.UserDaoImpl.save(com.wangj.spring.model.User))") @After("pointcutMethod()") // 方法执行完切入即使方法产生了异常 public void after() { System.out.println("method after(finally)"); } @AfterReturning("pointcutMethod()") // 方法正常执行完后切入 public void afterReturning() { System.out.println("method afterReturning"); } @AfterThrowing("pointcutMethod()") // 方法抛出异常时切入 public void afterThrowing() { System.out.println("method afterThrowing"); } @Around("myMethod()") // 环绕即在方法前、后都可以加逻辑 public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("method around start"); pjp.proceed(); System.out.println("method around end"); } }
四、使用XML方式实现AOP
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:annotation-config /> <context:component-scan base-package="com.wangj.spring" /> <!-- <aop:aspectj-autoproxy /> --> <bean id="logInterceptor" class="com.wangj.spring.aop.LogInterceptor2"></bean> <aop:config> <aop:pointcut id="daoPointcut" expression="execution(public * com.wangj.spring.dao..*.*(..)))"></aop:pointcut> <aop:aspect id="logAspect" ref="logInterceptor"> <!-- <aop:before method="before" pointcut="execution(public * com.wangj.spring.dao..*.*(..))" /> --> <!-- 不使用全局pointcut --> <aop:before method="before" pointcut-ref="daoPointcut" /> <aop:after method="after" pointcut-ref="daoPointcut" /> <aop:after-returning method="afterReturning" pointcut-ref="daoPointcut" /> <aop:after-throwing method="afterThrowing" pointcut-ref="daoPointcut"/> <aop:around method="aroundMethod" pointcut-ref="daoPointcut"/> </aop:aspect> </aop:config> </beans>