【发布时间】:2019-04-25 15:44:40
【问题描述】:
代理设置似乎在 Spring-Boot 1.5+ 和 2.+ 之间发生了变化。
在 Spring 1.5.20 中使用 @EnableAspectJAutoProxy(proxyTargetClass = false) 或仅使用 @EnableAspectJAutoProxy 甚至没有注释 @EnableAspectJAutoProxy 我会得到一个 JdkDynamicAopProxy。 并且使用 @EnableAspectJAutoProxy(proxyTargetClass = true) 我将获得 CGLIB 增强类。好的,一切都好。
使用与 Spring 2.1.4 相同的代码,无论配置如何,我都能得到一个 CGLIB 增强的 ServiceImpl。
我没有设法在 Spring 2+ 中使用 JdkDynamicAopProxy 代理。
还有办法吗?
这是我的代码:
@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");
}
}
【问题讨论】:
-
我认为Spring Boot issue #12194 解释了您的问题。很抱歉这个坏消息。
-
是的,我看到了这些问题。他们似乎说当您混合不同的代理意图时会出现问题:事务 + aspectj + ...但在我的示例中,上面只有一种方法,并且已经存在问题!如果他们不能修复它,他们应该删除属性来设置它,因为它们不再工作了......
标签: java spring spring-boot spring-aop