Spring aspect 两种方式实现五种增强
我们都知道spring中AOP(面向切面编程)支持多种增强类型,使之我们能更注重核心业务,而那些交叉业务(切面)则由增强来处理,我们来看一下配置增强的两种方式:注解和XML
注解:
1.引入jar包(上传图片不能用,只能粘贴了)
aopalliance.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
log4j-1.2.16.jar
spring-aop-4.2.0.RELEASE.jar
spring-aspects-4.2.0.RELEASE.jar
spring-beans-4.2.0.RELEASE.jar
spring-context-4.2.0.RELEASE.jar
spring-core-4.2.0.RELEASE.jar
spring-expression-4.2.0.RELEASE.jar
spring-test-4.2.0.RELEASE.jar
2.cn.aop---MyAspect.java(我们可以注释其他一个个测,因为抛出异常增强需要在UserdaoImpl.java中构建一个错误)
package cn.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class MyAspect { //前置 @Before("execution(public * *(..))")//execution(public * *(..))所有方法增强 public void beforeAspect(){ System.out.println("前置增强"); } //后置 @AfterReturning("execution(public * *(..))") public void AfterReturningAspect(){ System.out.println("后置增强"); } //环绕 /*@Around("execution(public * *(..))") public void AroundAspect(ProceedingJoinPoint po){ System.out.println("环绕前增强"); try { po.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("环绕后增强"); }*/ //异常 @AfterThrowing("execution(public * *(..))") public void AfterThrowingAspect(){ System.out.println("异常增强"); } //最终 @After("execution(public * *(..))") public void AfterAspect(){ System.out.println("最终增强"); } }
3.cn.entity--Userdao.java
package cn.entity; public interface Userdao { public void dofirst(); public void dosecond(); }
4.cn.entity--UserdaoImpl.java
package cn.entity; public class UserDaoImpl implements Userdao{ @Override public void dofirst() { // TODO Auto-generated method stub System.out.println("例子一"); } @Override public void dosecond() { // TODO Auto-generated method stub System.out.println("例子二"); } }
5.cn.test--ASpectTest.java
package cn.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.entity.Userdao; public class ASpectTest { @Test public void AspectTestBefore(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Userdao dao = (Userdao)context.getBean("daoimpl"); dao.dofirst(); dao.dosecond(); } }
6.applicationContext.xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <!-- 目标对象--> <bean id="daoimpl" class="cn.entity.UserDaoImpl"></bean> <!-- 增强 --> <bean class="cn.aop.MyAspect"></bean> <aop:aspectj-autoproxy/> </beans>
XML:()
其他不变,cn.aop---MyAspect.java这个类改为(注解去掉,变为普通类):
package cn.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class MyAspect { //前置 public void beforeAspect(){ System.out.println("前置增强"); } //后置 public void AfterReturningAspect(){ System.out.println("后置增强"); } public void AroundAspect(ProceedingJoinPoint po){ System.out.println("环绕前增强"); try { po.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("环绕后增强"); } //异常 public void AfterThrowingAspect(){ System.out.println("异常增强"); } //最终 public void AfterAspect(){ System.out.println("最终增强"); } }
applicationContext.xml改变配置
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <!-- 目标对象--> <bean id="daoimpl" class="cn.entity.UserDaoImpl"></bean> <!-- 增强 --> <bean id="aspectAdvice" class="cn.aop.MyAspect"></bean> <aop:config> <aop:pointcut expression="execution(public * *(..))" id="allpointcut"/> <aop:aspect ref="aspectAdvice"> <!-- 前置 --> <aop:before method="beforeAspect" pointcut-ref="allpointcut"/> <!-- 后置 --> <aop:after-returning method="AfterReturningAspect" pointcut-ref="allpointcut"/> <!-- 环绕 --> <aop:around method="AroundAspect" pointcut-ref="allpointcut"/> <!-- 异常抛出 --> <aop:after-throwing method="AfterThrowingAspect" pointcut-ref="allpointcut"/> <!-- 最终增强 --> <aop:after method="AfterAspect" pointcut-ref="allpointcut"/> </aop:aspect> </aop:config> </beans>
今天就到这里!!!