gpt4 book ai didi

java - 没有 @EnableAspectJAutoProxy,Spring AOP 能工作吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:17:08 29 4
gpt4 key购买 nike

我正在学习 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。

关于java - 没有 @EnableAspectJAutoProxy,Spring AOP 能工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48625149/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com