7
votes

The proxification setup seems to have changed between Spring-Boot 1.5+ and 2.+.

In Spring 1.5.20 with @EnableAspectJAutoProxy(proxyTargetClass = false) or just @EnableAspectJAutoProxy or even no annotation @EnableAspectJAutoProxy I would get a JdkDynamicAopProxy. And with @EnableAspectJAutoProxy(proxyTargetClass = true) I would get CGLIB enhanced classes. OK all good.

With the same code with Spring 2.1.4, I get a CGLIB enhanced ServiceImpl all the time whatever the config.

I did not managed to have JdkDynamicAopProxy proxies with Spring 2+.

Is there still a way to do it ?

Here is my code:

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = false)
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        MyService service = context.getBean(MyService.class);
        service.execute();
    }
}


@Aspect
@Component
public class ChronoAspect {
    @Around("execution(* com.example.demo.service..*.*(..))")
    public Object chronoAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        // .....
    }

}

public interface MyService {
    void execute();
}

@Component
public class ServiceImpl implements MyService {
    public void execute() {
        System.out.println("Hello execute from Service.execute");
    }
}
1
I think that Spring Boot issue #12194 explains your problem. Sorry for the bad news. - kriegaex
yes I saw those issues. They seem to say that problems arise when you mix different proxy intentions : transaction + aspectj + ... But in my example, above there is only one way of doing it and there is already a problem ! If they can't fix it, they should remove the properties to set it, because they don't work anymore... - Gauthier Peel

1 Answers

2
votes

It's a known issue.

Currently you can only set

spring.aop.proxy-target-class=false

in your config file to force it to use JDKProxy.