【问题标题】:How to apply spring aop aspect on a prototype scoped bean如何在原型作用域bean上应用spring aop方面
【发布时间】:2021-01-13 01:10:18
【问题描述】:

如何在原型作用域 bean 上应用 spring aop 方面

spring 方面是否不适用于原型作用域 bean? 我有一个带有几个构造函数参数的原型范围 bean。 bean 在运行时使用这些参数进行实例化。

我的spring配置是这样的-

@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 上编织建议?

感谢您对此提供任何帮助/建议。

【问题讨论】:

    标签: java spring aspectj spring-aop aspect


    【解决方案1】:

    我在 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>
    

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 2016-05-11
      • 2014-03-25
      • 1970-01-01
      相关资源
      最近更新 更多