问题描述
如何在原型范围内的bean上应用spring aop方面
spring方面不适用于原型作用域bean吗? 我有一个带有两个构造函数参数的原型作用域bean。 Bean在运行时使用这些参数实例化。
我的弹簧配置是这样的-
@Configuration
@EnableAspectJAutoproxy
public class SpringConfiguration {
@Bean
@Scope("prototype")
public PrototypeBean prototypeBean(SomeDTO dtoArg1,OtherDTO dtoArg2) {
return new PrototypeBean(dtoArg1,dtoArg2);
}
@Bean
public TestAspect testAspect() {
return new TestAspect();
}
}
我正在通过applicationContext在代码中获取bean-PrototypeBean,像这样-
applicationContext.getBean(PrototypeBean.class,dtoArg1,dtoArg2);
但是令人惊讶的是,方面没有在原型bean的joinpoint方法的调用上执行。我确信我创建的切入点是正确的,因为在eclipse中,AspectJ插件在joinPoint方法上显示了AspectJ引用的可视标记,这表明切入点是正确的,但不确定为什么在运行时无法在运行时执行切入点PrototypeBean的joinpoint方法被调用。
我以不正确的方式接近容器以获取bean还是容器没有机会在此原型bean上编织建议?
赞赏是否可以提供任何帮助/建议。
解决方法
我在Spring Boot 2.3.4中尝试了同样的方法,并且工作正常。
Here是仓库。请确保您具有以下依存关系,EnableAspectJAutoProxy
才能正常工作。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<scope>compile</scope>
</dependency>