在上篇文章《springAOP和AspectJ有关系吗?如何使用springAOP面向切面编程》中遗留了一个问题,那就是在springboot中使用springAOP需要加@EnableAspectJAutoProxy注解吗,网上很多都说需要加这个注解,但是有些情况却是不需要加,就比如我下面的例子,这是为什么,难道网上都说错了吗?

一、效果演示

以下面的例子演示,

业务类,

UserService.java

package com.my.template.service;

import com.my.template.entity.User;
import org.springframework.stereotype.Service; /**
* @date 2022/8/9 15:28
*/
@Service
public class UserService implements Us{
@Override
public void saveUser(User user){
System.out.println("保存user对象到数据库:"+user);
}
}

切面类,

LogAspect.java

package com.my.template.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* @date 2022/8/11 14:12
*/
@Component
@Aspect
public class LogAspect {
@Pointcut("execution(* com.my.template.service.UserService.*(..))")
public void pointCut(){ }
@Before(value = "pointCut()")
public void before(JoinPoint joinPoint){
System.out.println("方法执行前-20220816");
} @AfterReturning(value = "pointCut()")
public void after(JoinPoint joinPoint){
System.out.println("方法执行后-20220816");
}
}

测试的controller

UserController.java

package com.my.template.controller;

import com.my.template.entity.User;
import com.my.template.service.Us;
import com.my.template.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @date 2022/8/9 15:35
*/
@RestController
public class UserController {
@Autowired
private Us us;
@RequestMapping("/saveUser")
public String saveUser(){ User user=new User();
user.setId("1");
user.setName("张三");
us.saveUser(user);
return "success";
}
}

sprinboot的启动类,

BootServer.java

package com.my.template;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource; /**
* 启动类
* @date 2022/6/3 21:32
*/
@SpringBootApplication()
public class BootServer {
public static void main(String[] args) {
try {
SpringApplication.run(BootServer.class);
}catch (Exception e){
e.printStackTrace();
}
}
}

测试结果如下,

2022-08-16 22:30:44.082  INFO 25716 --- [nio-9099-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 6 ms
方法执行前-20220816
保存user对象到数据库:User{name='张三', id='1'}
方法执行后-20220816

从上面的测试结果来看,没有加@EnableAspectJAutoProxy注解,但是AOP生效了,这是为什么?

二、为什么不加@EnableAspectJAutoProxy切面生效

关于这个问题我排查了很久,最后在依赖中找到了原因,看下pom文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>springTemplate</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--spring-boot的web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<!--自定义的starter-->
<dependency>
<groupId>org.example</groupId>
<artifactId>customer-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--使用springAOP需要引入该依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

在依赖中有spring-boot-starter-web的依赖,该依赖有下面的依赖,

会引入spring-boot-autoconfigure的依赖,这是自动装配的依赖,也就是会读取其下的spring.factories文件,在该文件中有下面的配置,

没错就是因为AopAutoConfiguration类的问题。下面看具体原因。

三、原因分析

要看具体原因,我们就要打开AopAutoConfiguration这个类看下,

先看注释吧,注释中说AopAutoConfiguration等同于@EnableAspectJAutoProxy注解,也就是该类起的作用和@EnableAspectJAutoProxy是一样的,再看该类上的注解,重点看@ConditionalOProperty注解中的内容,意思是如果在配置文件中有”spring.aop.auto“的配置,如果不配置为true,否则可以配置为false,现在我的配置文件中是没有该配置项的,

server.port=9099
spring.datasource.type=com.mysql.cj.jdbc.MysqlDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource..driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=yh_dev
spring.datasource..password=DvpJe2x
spring.datasource.url=jdbc:mysql://10.0.0.37:3306/channel_center
#?????
my.customer.name=hello
my.customer.code=autoconfiguration

那么我现在增加该配置,并设置为false,

server.port=9099
spring.datasource.type=com.mysql.cj.jdbc.MysqlDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource..driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=yh_dev
spring.datasource..password=DvpJe2x
spring.datasource.url=jdbc:mysql://10.0.0.37:3306/channel_center
#?????
my.customer.name=hello
my.customer.code=autoconfiguration
spring.aop.auto=false

重启服务之后,看测试结果,

从测试结果可以看到springAOP没有起作用,现在在启动类上加上@EnableAspectJAutoProxy注解,看下测试结果,

从上面的测试结果可以看到,添加了@EnableAspectJAutoProxy注解springAOP生效了。

综上,在springboot环境下,由于存在spring-boot-autoconfigure依赖,默认会注入AopAutoConfiguration配置类,该类的作用等同于@EnableAspectJAutoProxy注解,所以在这种情况下可以不加@EnableAspectJAutoProxy注解,AopAutoConfiguration可以通过spring.aop.auto属性控制;

四、总结

本文主要分析了在springboot环境下,不加@EnableAspectJAutoProxy注解springAOP仍然生效的问题。为了保险期间请一律加上@EnableAspetJAutoProxy注解。

  1. AopAutoConfiguration类等同于@EnableAspectJAutoProxy注解;
  2. spring.aop.auto=ture/false属性可以控制AopAutoConfiguration类是否生效;

使springAOP生效不一定要加@EnableAspectJAutoProxy注解的更多相关文章

  1. JQuery - 动态添加Html后,如何使CSS生效,JS代码可用?

    今天在开发JQuery Mobile程序时候,需要从服务器取得数据,随后显示在页面上的Listview控件中,数据完整获取到了,也动态添加到Listview控件中,但是数据对应的CSS没有任何效果了, ...

  2. 更新gitignore后如何使其生效

    Files already tracked by Git are not affected; Git - gitignore Documentation https://git-scm.com/doc ...

  3. 【Spring注解驱动开发】二狗子让我给他讲讲@EnableAspectJAutoProxy注解

    写在前面 最近,二狗子入职了新公司,新入职的那几天确实有点飘.不过慢慢的,他发现他身边的人各个身怀绝技啊,有Spring源码的贡献者,有Dubbo源码的贡献者,有MyBatis源码的贡献者,还有研究A ...

  4. Linux安装redis,启动配置不生效(指定启动加载配置文件)

    一.今天有个同学问我,为什么明明安装了redis,修改了配置,启动的时候,配置还是不生效.如下图是安装后的redis文件图. 二.想加载上图中的redis.conf,进入到src中寻找到启动文件red ...

  5. 使ViewStub 来提高UI的加载的性能

    首先看下API中的ViewStub 根据的文档的说明,ViewStub是一种默认不可见的试图,它没有大小,所以不能被改变,也不能通过某些把viewstub添加到布局当中来, 不过我们可以使用infla ...

  6. SpringBoot 中使用shiro注解使之生效

    在shiroConfig配置类中增加如下代码: /** * 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro ...

  7. Android WebView导入HTML使Js生效的方法

    WebSettings ws = webview.getSettings(); ws.setJavaScriptEnabled(true);//加上这句 webview.loadDataWithBas ...

  8. 不停止nginx服务,使配置文件生效

    ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}' ...

  9. 使gitignore生效

    git rm -r --cached . // 删除本地缓存 git add . // 添加要提交的文件 初次提交直接声明gitignore并提交就可以: 非初次提交,改动的gitignore要进行上 ...

随机推荐

  1. MySQL - 数据库设计步骤

    需求分析:分析用户的需求,包括数据.功能和性能需求. 概念结构设计:主要采用E-R模型进行设计,包括画E-R图. 逻辑结构设计:通过将E-R图转换成表,实现从E-R模型到关系模型的转换,进行关系规范化 ...

  2. DevOps落地实践点滴和踩坑记录-(1)

    记录初衷 本人一直在从事企业内DevOps落地实践的工作,走了不少弯路,也努力在想办法解决面临的问题,期间也经历过不少人和事情,最近突然有想法把经历过的,不管好的不好的都记录下来,分享给和我一样的一线 ...

  3. 我的 Java 学习&面试网站又又又升级了!

    晚上好,我是 Guide. 距离上次介绍 JavaGuide 新版在线阅读网站已经过去 7 个多月了(相关阅读:官宣!我升级了!!!),这 7 个多月里不论是 JavaGuide 的内容,还是 Jav ...

  4. php个性代码注释

      // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\____ // . ' \ ...

  5. crane:字典项与关联数据处理的新思路

    前言 在我们日常开发中,经常会遇到一些烦人的数据关联和转换问题,比如典型的: 对象属性中个有字典 id,需要获取对应字典值并填充到对象中: 对象属性中有个外键,需要关联查询对应的数据库表实体,并获取其 ...

  6. UiPath Orchestrator安装步骤

    UiPath Orchestrator安装步骤 答案在这 https://rpazj.com/thread-219-1-1.html

  7. MOEAD实现、基于分解的多目标进化、 切比雪夫方法-(python完整代码)

    确定某点附近的点 答:每个解对应的是一组权重,即子问题,红点附近的四个点,也就是它的邻居怎么确定呢?由权重来确定,算法初始化阶段就确定了每个权重对应的邻居,也就是每个子问题的邻居子问题.权重的邻居通过 ...

  8. Java 将HTML转为Word

    本文以Java代码为例介绍如何实现将HTML文件转为Word文档(.docx..doc).在实际开发场景中可参考此方法来转换.下面详细方法及步骤. 在编辑代码前,请先在程序中导入Spire.Doc.j ...

  9. NC24724 [USACO 2010 Feb S]Chocolate Eating

    NC24724 [USACO 2010 Feb S]Chocolate Eating 题目 题目描述 Bessie has received \(N (1 <= N <= 50,000)\ ...

  10. NC24866 [USACO 2009 Dec S]Music Notes

    NC24866 [USACO 2009 Dec S]Music Notes 题目 题目描述 FJ is going to teach his cows how to play a song. The ...