java Spring AOP 可以在没有 @EnableAspectJAutoProxy 的情况下工作吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48625149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring AOP works without @EnableAspectJAutoProxy?
提问by lukeg
I am learning Spring (currently its AOP framework). Even though all sources I've read say that to enable AOP one needs to use @EnableAspectJAutoProxy
annotation (or its XML counterpart) my code seems to work with annotation commented out. Is that because I use Lombok or Spring Boot (v. 1.5.9.RELEASE, dependent on Spring v. 4.3.13.RELEASE)?
我正在学习 Spring(目前是它的 AOP 框架)。尽管我读过的所有来源都说要启用 AOP 需要使用@EnableAspectJAutoProxy
注释(或其 XML 对应物),但我的代码似乎可以使用注释掉的注释。那是因为我使用 Lombok 或 Spring Boot(v. 1.5.9.RELEASE,依赖于 Spring v. 4.3.13.RELEASE)?
Minimal example follows:
最小示例如下:
build.gradle
构建.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
group = 'lukeg'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compileOnly('org.projectlombok:lombok')
compile("org.aspectj:aspectjweaver:1.8.11")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
ApplicationConfiguration.java(note the AOP annotation is commented out)
ApplicationConfiguration.java(注意AOP注解被注释掉了)
package lukeg;
import org.springframework.context.annotation.*;
@Configuration
@ComponentScan
//@EnableAspectJAutoProxy
public class ApplicationConfiguration {
@Bean
TestComponent testComponent() {
return new TestComponent();
}
}
LearnApplication.java
学习应用程序
package lukeg;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootApplication
public class LearnApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(LearnApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
TestComponent testComponent = context.getBean(TestComponent.class);
System.out.println(""+testComponent);
}
}
LoggerHogger.java
记录器Hogger.java
package lukeg;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LoggerHogger {
@Pointcut("execution(* lukeg*.*.toString(..))")
public void logToString() {}
@Before("logToString()")
public void beforeToString () {
System.out.println("Before toString");
}
}
TestComponent.java
测试组件.java
package lukeg;
import lombok.Data;
@Data
public class TestComponent {
}
回答by Ian Mc
The @SpringBootApplication
annotation contains the @EnableAutoConfiguration
annotation. This autoconfiguration is one of the attractions of Spring Boot and makes configuration simpler. The auto configuration uses @Conditional
type annotations (like @ConditionalOnClass
and @ConditionalOnProperty
) to scan the classpath and look for key classes that trigger the loading of 'modules' like AOP.
该@SpringBootApplication
注释包含@EnableAutoConfiguration
注释。这种自动配置是 Spring Boot 的吸引力之一,使配置更简单。自动配置使用@Conditional
类型注释(如@ConditionalOnClass
和@ConditionalOnProperty
)来扫描类路径并查找触发加载“模块”(如 AOP)的关键类。
Here is an example AopAutoConfiguration.java
这是一个示例AopAutoConfiguration.java
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.Advice;
import org.aspectj.weaver.AnnotatedElement;
@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
AnnotatedElement.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
public static class JdkDynamicAutoProxyConfiguration {
}
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
public static class CglibAutoProxyConfiguration {
}
}
As you can see, if you add one of the above aop classes to your class path (or property), Spring will detect it and effectively behave as if you had the @EnableAspectJAutoProxy annotation on your main class.
如您所见,如果您将上述 aop 类之一添加到您的类路径(或属性)中,Spring 将检测到它并有效地表现得好像您在主类上有 @EnableAspectJAutoProxy 注释一样。
Your project has a file LoggerHogger which has an @Aspect.
您的项目有一个文件 LoggerHogger,其中包含一个 @Aspect。