[英]Spring AOP works without @EnableAspectJAutoProxy?
我正在學習Spring(目前是AOP框架)。 即使我讀過的所有資源都說要啟用AOP需要使用@EnableAspectJAutoProxy
注釋(或其XML對應物),我的代碼似乎可以使用注釋注釋掉。 是因為我使用Lombok或Spring Boot(v.1.5.9.RELEASE,依賴於Spring v.4.3.13.RELEASE)?
最小的例子如下:
的build.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 (注意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
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
package lukeg;
import lombok.Data;
@Data
public class TestComponent {
}
@SpringBootApplication
批注包含@EnableAutoConfiguration
批注。 這種自動配置是Spring Boot的吸引力之一,使配置更簡單。 自動配置使用@Conditional
類型注釋(如@ConditionalOnClass
和@ConditionalOnProperty
)來掃描類路徑並查找觸發加載“模塊”(如AOP)的鍵類。
這是一個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 {
}
}
正如您所看到的,如果您將上述aop類之一添加到類路徑(或屬性)中,Spring將檢測它並有效地表現得就像在主類上有@EnableAspectJAutoProxy注釋一樣。
您的項目有一個文件LoggerHogger,它有一個@Aspect。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.