Spring AOP没有@EnableAspectJAutoProxy可以工作吗? [英] Spring AOP works without @EnableAspectJAutoProxy?

查看:1245
本文介绍了Spring AOP没有@EnableAspectJAutoProxy可以工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring(当前是其AOP框架).即使我阅读过的所有消息都说要启用AOP,也需要使用@EnableAspectJAutoProxy注释(或其XML副本),但我的代码似乎可以注释掉注释.是因为我使用了Lombok还是Spring Boot(v.1.5.9.RELEASE,取决于Spring v.4.3.13.RELEASE)?

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)?

最小示例如下:

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注释已被注释掉)

ApplicationConfiguration.java (note the AOP annotation is commented out)

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)加载的键类.

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.

这是一个示例

如您所见,如果将上述aop类之一添加到类路径(或属性)中,Spring将检测到它并有效地表现出好像在主类上具有@EnableAspectJAutoProxy批注.

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.

您的项目有一个LoggerHogger文件,其中有一个@Aspect.

Your project has a file LoggerHogger which has an @Aspect.

这篇关于Spring AOP没有@EnableAspectJAutoProxy可以工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆