Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP works without @EnableAspectJAutoProxy?

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

Minimal example follows:

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 (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 {
}
like image 602
lukeg Avatar asked Feb 05 '18 14:02

lukeg


People also ask

What is the use of EnableAspectJAutoProxy?

Annotation Type EnableAspectJAutoProxy Users can control the type of proxy that gets created for FooService using the proxyTargetClass() attribute. The following enables CGLIB-style 'subclass' proxies as opposed to the default interface-based JDK proxy approach.

How AOP works internally in spring?

Spring AOP is proxy-based. Spring uses either JDK proxies (preferred wheneven the proxied target implements at least one interface) or CGLIB proxies (if the target object does not implement any interfaces) to create the proxy for a given target bean.


1 Answers

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.

Here is an example 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.

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

like image 138
Ian Mc Avatar answered Oct 20 '22 10:10

Ian Mc