Spring中使用AOP及SpringBoot集成AOP
一、Spring中使用AOP
引入Aspect
1 <dependency> 2 <groupId>org.aspectj</groupId> 3 <artifactId>aspectjweaver</artifactId> 4 <version>${aspectj.version}</version> 5 </dependency>
创建用于拦截的bean
1 public class TestBean { 2 private String message = "test bean"; 3 4 public String getMessage() { 5 return message; 6 } 7 8 public void setMessage(String message) { 9 this.message = message; 10 } 11 12 public void test(){ 13 System.out.println(this.message); 14 } 15 }
创建Advisor
1 @Aspect 2 public class AspectJTest { 3 @Pointcut("execution(* *.test(..))") 4 public void test(){ 5 } 6 7 @Before("test()") 8 public void beforeTest(){ 9 System.out.println("beforeTest"); 10 } 11 12 @Around("test()") 13 public Object aroundTest(ProceedingJoinPoint p){ 14 System.out.println("around.....before"); 15 Object o = null; 16 try{ 17 o = p.proceed(); 18 }catch(Throwable e){ 19 e.printStackTrace(); 20 } 21 System.out.println("around.....after"); 22 return o; 23 } 24 25 @After("test()") 26 public void afterTest() 27 { 28 System.out.println("afterTest"); 29 } 30 }
创建配置文件
要在Spring中开启AOP功能,还需要在配置文件中作如下声明,开启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.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"> 8 9 <aop:aspectj-autoproxy/> 10 <bean id="test" class="com.yhl.myspring.demo.aop.TestBean"> 11 <property name="message" value="这是一个苦逼的程序员"/> 12 </bean> 13 <bean id="aspect" class="com.yhl.myspring.demo.aop.AspectJTest"/> 14 </beans>
注解开启AOP
开启AOP<aop:aspectj-autoproxy/>也可以使用注解的方式,如下,使用@EnableAspectJAutoProxy配置在任何一个@Configratrion或者@Component上

二、SpringBoot集成AOP
添加pom依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-aop</artifactId> 4 </dependency>
引入了AOP的场景启动器,我们点击去看看

还是引入了spring-aop和aspectj的依赖,和我们Spring集成AOP是引入了相同的包,接着我们直接就可以创建Advisor了,如上AspectJTest这个类,但是我们并没有通过@EnableAspectJAutoProxy开启AOP呢?那是因为AOP的自动配置类帮我们开启了
AopAutoConfiguration
一旦导入了spring-boot-starter-aop依赖后,SpringBoot就会启动AOP的自动配置类AopAutoConfiguration:

我们来看看AopAutoConfiguration这个自动配置类
1 @Configuration 2 @ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class }) 3 @ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true) 4 public class AopAutoConfiguration { 5 6 @Configuration 7 //使用注解开启AOP功能 8 @EnableAspectJAutoProxy(proxyTargetClass = false) 9 @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = true) 10 public static class JdkDynamicAutoProxyConfiguration { 11 12 } 13 14 @Configuration 15 //使用注解开启AOP功能 16 @EnableAspectJAutoProxy(proxyTargetClass = true) 17 @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = false) 18 public static class CglibAutoProxyConfiguration { 19 20 } 21 22 }
不管使用jdk代理还是cglib代理,都有@EnableAspectJAutoProxy注解,所以只要导入spring-boot-starter-aop依赖后,就自动帮我们开启了AOP,我们可以直接添加切面使用AOP了。
@EnableAspectJAutoProxy这个注解是整个AOP的灵魂,其作用和<aop:aspectj-autoproxy/>是一样的。
可以看看其源码分析spring5 源码深度解析----- AOP的使用及AOP自定义标签
附:@Aspect 实现样例代码
import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * */ @Aspect @Component public class HandlerMonitor { private static Log logger = LogFactory.getLog(HandlerMonitor.class); @Pointcut("execution(public com.xxx.xxx.commons.base.lang.JsonData com.ke.xxx.xxx.controller..*.*(..))") public void pointCut() {} @Around("pointCut()") public Response processHandler(ProceedingJoinPoint pjp) { Response response = null; logger.info(LogUtils.getCommLog( String.format("The process method: %s", pjp.getSignature().getName()))); // 开始时间 Date startDate = new Date(); Request cRequest = null; try { cRequest = Request.getRequest(); logger.info(LogUtil.formatLog(String.format("[REQUEST]%s %s", cRequest.getUrl(), cRequest.getRequest().getHttpParams()))); response = (JsonData) pjp.proceed(); } catch (Exception e) { response = new JsonData(); // 判断是否为BaseException异常及其子异常 if (BaseException.class.isAssignableFrom(e.getClass())) { BaseException baseException = (BaseException) e; logger.error(LogUtils.getCommLog(baseException.toString())); } else { logger.error(LogUtils.getCommLog(e.toString()), e); } } catch (Throwable e) { response = new JsonData(); logger.error(LogUtils.getCommLog(e.toString()), e); } // 获取处理时间 int costTime = this.getAndFillCostTime(startDate); // 日志打印 logger.info(LogUtils.getResponseLog(costTime, response.toString(), cRequest.getUrl())); try { logger.info(LogUtils.getCommLog(String.format("当前登录人ID和姓名:%s %s", libraAssist.getUserId(), libraAssist.getUserName()))); } catch (Exception e) { logger.error(LogUtils.getCommLog(String.format("获取当前登录人信息异常"))); } return response; } /** * 获取处理时间 * * @param meta * @param startTime * @return */ public int getAndFillCostTime(Date startTime) { if (startTime == null) { return 0; } Date endDate = new Date(); int costTime = (int) (endDate.getTime() - startTime.getTime()); return costTime; } }

浙公网安备 33010602011771号