使springAOP生效不一定要加@EnableAspectJAutoProxy注解
在上篇文章《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注解。
- AopAutoConfiguration类等同于@EnableAspectJAutoProxy注解;
- spring.aop.auto=ture/false属性可以控制AopAutoConfiguration类是否生效;

使springAOP生效不一定要加@EnableAspectJAutoProxy注解的更多相关文章
- JQuery - 动态添加Html后,如何使CSS生效,JS代码可用?
今天在开发JQuery Mobile程序时候,需要从服务器取得数据,随后显示在页面上的Listview控件中,数据完整获取到了,也动态添加到Listview控件中,但是数据对应的CSS没有任何效果了, ...
- 更新gitignore后如何使其生效
Files already tracked by Git are not affected; Git - gitignore Documentation https://git-scm.com/doc ...
- 【Spring注解驱动开发】二狗子让我给他讲讲@EnableAspectJAutoProxy注解
写在前面 最近,二狗子入职了新公司,新入职的那几天确实有点飘.不过慢慢的,他发现他身边的人各个身怀绝技啊,有Spring源码的贡献者,有Dubbo源码的贡献者,有MyBatis源码的贡献者,还有研究A ...
- Linux安装redis,启动配置不生效(指定启动加载配置文件)
一.今天有个同学问我,为什么明明安装了redis,修改了配置,启动的时候,配置还是不生效.如下图是安装后的redis文件图. 二.想加载上图中的redis.conf,进入到src中寻找到启动文件red ...
- 使ViewStub 来提高UI的加载的性能
首先看下API中的ViewStub 根据的文档的说明,ViewStub是一种默认不可见的试图,它没有大小,所以不能被改变,也不能通过某些把viewstub添加到布局当中来, 不过我们可以使用infla ...
- SpringBoot 中使用shiro注解使之生效
在shiroConfig配置类中增加如下代码: /** * 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro ...
- Android WebView导入HTML使Js生效的方法
WebSettings ws = webview.getSettings(); ws.setJavaScriptEnabled(true);//加上这句 webview.loadDataWithBas ...
- 不停止nginx服务,使配置文件生效
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}' ...
- 使gitignore生效
git rm -r --cached . // 删除本地缓存 git add . // 添加要提交的文件 初次提交直接声明gitignore并提交就可以: 非初次提交,改动的gitignore要进行上 ...
随机推荐
- Clickhouse实时数仓建设
1.概述 Clickhouse是一个开源的列式存储数据库,其主要场景用于在线分析处理查询(OLAP),能够使用SQL查询实时生成分析数据报告.今天,笔者就为大家介绍如何使用Clickhouse来构建实 ...
- CSS元素的几种显示模式
元素的显示模式 元素的显示模式就是元素以生么方式进行显示,比如<div>自己占一行,比如一行可以放多个<span>. HTML元素一般分为块元素和行内元素. 块元素 常见的块元 ...
- 字符串的操作、 Math类
字符串的操作 我们先来定义一个字符串,如果来进行过去长度,获取内容. 我们来写一个小测试! public static void main(String[] args) { String aa = & ...
- 五分钟搞懂POM设计模式
转载请注明出处️ 作者:IT小学生蔡坨坨 原文链接:五分钟搞懂POM设计模式 大家好,我是IT小学生蔡坨坨. 今天,我们来聊聊Web UI自动化测试中的POM设计模式. 为什么要用POM设计模式 前期 ...
- VTK 在WINDOWS上的安装使用
参考:http://www.vtk.org/Wiki/VTK/Building/Windows#Step_5_-_Open_the_Visual_Studio_project
- 『忘了再学』Shell基础 — 32、Shell中test测试命令详解
目录 1.test测试命令 (1)test命令介绍 (2)test命令使用方式 (3)示例 2.按照文件类型进行判断 3.按照文件权限进行判断 4.两个文件之间进行比较 5.两个整数之间比较 6.字符 ...
- Jmter入门教程
Jmter入门教程 本文已同步到公众号,欢迎关注: 1. 简介 Apache JMeter是一款纯java编写负载功能测试和性能测试开源工具软件.相比Loadrunner而言,JMeter小巧轻便且免 ...
- GameFramework食用指南
1.框架简介 GF框架分两部分,GameFramework(GF)和UnityGameFramework(UGF): 通过接口的形式对Unity引擎进行了解耦: GF独立于Unity,具体业务逻辑实现 ...
- 纪念我逝去的n个小时
纪念我逝去的n个小时 某人的惨案要我擦屁股=.= #include <bits/stdc++.h> using namespace std; template<class T> ...
- 浅议.NET遗留应用改造
浅议.NET遗留应用改造 TLDR:本文介绍了遗留应用改造中的一些常见问题,并对改造所能开展的目标.原则.策略进行了概述. 一.背景概述 1.概述 或许仅"遗留应用"这个标题就比较 ...