java - 如何在原型作用域bean上应用spring aop方面
问题描述
如何在原型作用域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 的连接点方法的调用上执行。我确信我创建的切入点是正确的,因为在 Eclipse 中,aspectJ 插件在 joinPoint 方法上显示了 aspectJ 引用的可视标记,这表明切入点是正确的,但不确定为什么它在运行时没有被执行PrototypeBean 的 joinpoint 方法被调用。
我是否以不正确的方式接近容器来获取 bean 或者容器没有机会在这个原型 bean 上编织建议?
感谢是否可以提供任何帮助/建议。
解决方案
我用 Spring Boot 2.3.4 尝试了同样的事情,它工作得很好。
这是回购。确保您具有以下依赖项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>
推荐阅读
- python - 为什么只有 3 个参数时,python 说有 4 个参数?
- php - PHP在子方法中覆盖父返回类型
- powershell - sqldataadapter 在 powershell 中不起作用
- sql - 如何在 PostgreSQL 中找到数组的最后一个成员?
- hyperledger-fabric - 调用“channel.sendTransactionProposal”时的“gRPC failure=Status{code=UNAVAILABLE, description=io exception”
- azure-devops - Azure Pipeline 多阶段中针对特定阶段的计划触发器
- java - 在java中导入该类时,我们是导入该类的源代码还是字节码?
- javascript - 为什么我应该在 React Route 中使用扩展运算符?
- ruby-on-rails - 如何以可扩展的方式取消(软删除)数据库中的记录
- javascript - “元素”上不存在属性“焦点”?