3、AOP带来的好处::降低模块的耦合度;使系统容易扩展;更好的代码复用性
二、通过注解方式实现Spring的AOP
1.定义业务类
接口:
package cn.slimsmart.spring.demo.aop; public interface UserService { String save(String name); String update(String name); String delete(String name); }实现:
package cn.slimsmart.spring.demo.aop; import org.springframework.stereotype.Service; @Service <span style="font-family: Arial, Helvetica, sans-serif;">//使用自动注解的方式实例化并初始化该类</span> public class UserServiceImpl implements UserService{ @Override public String save(String name) { System.out.println("--------save"); return "save"; } @Override public String update(String name) { System.out.println("--------update"); System.out.println(1/0); return "update"; } @Override public String delete(String name) { System.out.println("--------delete"); return "delete"; } }2.定义切面类
package cn.slimsmart.spring.demo.aop; import org.aspectj.lang.JoinPoint; 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; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; //@Aspect : 标记为切面类 //@Pointcut : 指定匹配切点集合 //@Before : 指定前置通知,value中指定切入点匹配 //@AfterReturning :后置通知,具有可以指定返回值 //@AfterThrowing :异常通知 //@Around 环绕通知 环绕通知的方法中一定要有ProceedingJoinPoint 参数,与Filter中的 doFilter方法类似 //注意:前置/后置/异常通知的函数都没有返回值,只有环绕通知有返回值 @Component //使用自动注解的方式实例化并初始化该类 @Aspect public class TestInterceptor { //如果要设置多个切点可以使用 || 拼接 @Pointcut("execution(* cn.slimsmart.spring.demo.aop.UserServiceImpl.*(..))") private void anyMethod() { }// 定义一个切入点 @Before(value="anyMethod()") public void doBefore(JoinPoint joinPoint) { System.out.println("前置通知"); } @AfterReturning(value="anyMethod()",returning="result") public void doAfter(JoinPoint jp, String result) { System.out.println("后置通知"); } @After("anyMethod()") public void after() { System.out.println("最终通知"); } @AfterThrowing(value="execution(* cn.slimsmart.spring.demo.aop.*.*(..))",throwing="e") public void doAfterThrow(JoinPoint joinPoint, Throwable e) { System.out.println("异常通知"); } @Around("execution(* cn.slimsmart.spring.demo.aop.*.*(..))") public Object doBasicProfiling(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("进入环绕通知"); System.out.println("目标类名称:"+joinPoint.getTarget().getClass().getName()); System.out.println("方法名称:"+joinPoint.getSignature().getName()); System.out.println("方法参数:"+joinPoint.getArgs()); System.out.println("staticPart:"+ joinPoint.getStaticPart().toShortString()); System.out.println("kind:"+joinPoint.getKind()); System.out.println("sourceLocation:"+joinPoint.getSourceLocation()); Object object = joinPoint.proceed();// 执行该方法 System.out.println("退出方法"); return object; } }3.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <context:component-scan base-package="cn.slimsmart.spring.demo" /> <!-- 打开aop 注解 --> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>4.单元测试类
package cn.slimsmart.spring.demo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.slimsmart.spring.demo.aop.UserService; @RunWith(SpringJUnit4ClassRunner.class)//让junit工作在spring环境中 @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class SpringTest{ @Autowired UserService userService; @Test public void testStart(){ System.out.println("启动服务"); userService.delete("abc123"); System.out.println("===================="); userService.update("aaa"); } }
启动服务 进入环绕通知 目标类名称:cn.slimsmart.spring.demo.aop.UserServiceImpl 方法名称:delete 方法参数:[Ljava.lang.Object;@2de1c3f9 staticPart:execution(UserServiceImpl.delete(..)) kind:method-execution sourceLocation:org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@6ed745c2 前置通知 --------delete 退出方法 最终通知 后置通知 ==================== 进入环绕通知 目标类名称:cn.slimsmart.spring.demo.aop.UserServiceImpl 方法名称:update 方法参数:[Ljava.lang.Object;@1d370b4d staticPart:execution(UserServiceImpl.update(..)) kind:method-execution sourceLocation:org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@8c6fb37 前置通知 --------update 最终通知 异常通知注:pom.xml引入的jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.slimsmart.spring.demo</groupId> <artifactId>spring-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- <dependency> --> <!-- <groupId>org.springframework</groupId> --> <!-- <artifactId>spring-tx</artifactId> --> <!-- <version>4.1.4.RELEASE</version> --> <!-- </dependency> --> <!-- <dependency> --> <!-- <groupId>org.springframework</groupId> --> <!-- <artifactId>spring-jdbc</artifactId> --> <!-- <version>4.1.4.RELEASE</version> --> <!-- </dependency> --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
若不使用注解的方式,使用schema xml配置(也可以基于spring接口方式实现)的方式,如下参考:
<aop:config> <aop:aspect id="businessAspect" ref="testInterceptor"> <!-- 配置指定切入的对象 --> <aop:pointcut id="point_cut" expression="execution(* cn.slimsmart.spring.demo.aop.UserServiceImpl.*(..))" /> <!-- 前置通知 --> <aop:before method="doBefore" pointcut-ref="point_cut" /> <!-- 后置通知 returning指定返回参数 --> <aop:after-returning method="doAfter" pointcut-ref="point_cut" returning="result" /> <aop:around method="doAround" pointcut-ref="point_cut"/> <aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/> </aop:aspect> </aop:config>