【问题标题】:Configuring AspectJ aspects using Spring IoC with JavaConfig?使用带有 JavaConfig 的 Spring IoC 配置 AspectJ 方面?
【发布时间】:2014-04-04 02:54:15
【问题描述】:

根据 Spring 的文档Configuring AspectJ aspects using Spring IoC,为了配置 Spring IOC 的方面,必须将以下内容添加到 xml 配置中:

<bean id="profiler" class="com.xyz.profiler.Profiler"
      factory-method="aspectOf">
  <property name="profilingStrategy" ref="jamonProfilingStrategy"/>
</bean>

正如@SotiriosDelimanolis 所建议的那样,在 JavaConfig 中将其重写为以下内容应该可以工作:

@Bean
public com.xyz.profiler.Profiler profiler() {
    com.xyz.profiler.Profiler profiler = com.xyz.profiler.Profiler.aspectOf();
    profiler.setProfilingStrategy(jamonProfilingStrategy()); // assuming you have a corresponding @Bean method for that bean
    return profiler;
}

但是,这似乎只有在 Profiler 方面是用原生 aspectj .aj 语法编写时才有效。如果它是用 Java 编写并带有 @Aspect 注释的,我会收到以下错误消息:

没有为 Profiler 类型定义 aspectOf() 方法

对于使用@AspectJ 语法编写的方面,是否有一种等效的方式来使用 JavaConfig 编写此内容?

【问题讨论】:

    标签: java spring aop aspectj spring-java-config


    【解决方案1】:

    原来有一个org.aspectj.lang.Aspects 类专门为此目的提供。看来aspectOf() 方法是由 LTW 添加的,这就是为什么它在 XML 配置中可以正常工作,但在编译时却不行。

    为了绕过这个限制,org.aspectj.lang.Aspects 提供了一个aspectOf() 方法:

    @Bean
    public com.xyz.profiler.Profiler profiler() {
        com.xyz.profiler.Profiler profiler = Aspects.aspectOf(com.xyz.profiler.Profiler.class);
        profiler.setProfilingStrategy(jamonProfilingStrategy()); // assuming you have a corresponding @Bean method for that bean
        return profiler;
    }
    

    希望这对将来的其他人有所帮助。

    【讨论】:

    • 这个 \@Bean 是在哪里定义的?在配置类中?您是否仍然使用 @Aspect 注释您的 Advice 并在 Configuration 类上使用 \@EnableAspectJAutoProxy ?我已经尝试过了,但它看起来不像 AspectJ 编译时编织正在启动。这是我的示例代码:github.com/jigishpa/spring-samples/tree/master/aop/hello
    • 我使用带有 @EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED) 的 spring java 配置以及 aop.xml 的 部分中列出的方面。当我们使用 Aspects.aspectOf 定义方面 bean 时,出现以下错误:线程“main”中的异常 org.aspectj.lang.NoAspectBoundException:初始化 storey.jeff.MyAspect 时出现异常:java.lang.NoSuchMethodException:storey.jeff.MyAspect .aspectOf() 我认为这意味着在创建 bean 之前还没有编织方面。关于如何解决这个问题的任何想法?
    【解决方案2】:

    是否有使用 JavaConfig 的等效方法来编写此代码?

    几乎总是。

    @Bean
    public com.xyz.profiler.Profiler profiler() {
        com.xyz.profiler.Profiler profiler = com.xyz.profiler.Profiler.aspectOf();
        profiler.setProfilingStrategy(jamonProfilingStrategy()); // assuming you have a corresponding @Bean method for that bean
        return profiler;
    }
    

    factory-methodInstantiation with a static factory method 的文档中进行了解释。

    【讨论】:

    • 我试过了,但由于aspectOf() 未定义,因此出现编译时错误。 “类型 Profiler 的方法 aspectOf() 未定义”。我以为我做错了。
    • @EricB。我不太了解 AspectJ 来帮助您,但是如果您没有为您的 Profiler 类提供 aspectOf 方法并且 AspectJ 没有通过其他方式(字节码操作)提供它,那么就没有你可以做很多事情。您在问题中的 XML bean 定义与上面的 @Bean 方法完全相同。我将假设您在 AspectJ 设置中遗漏了一些东西。
    • 我不确定,但我认为这可能与被定义为@Aspect 的方面有关,Java 编译器将其与常规 java 类混淆。如果我将方面编写为.aj 文件,它似乎可以按预期工作。问题是我不知道如何解决这个问题。我更新了我的问题以反映新信息。
    • @EricB。我将留下我的答案,因为它回答了Is there an equivalent way of writing this using JavaConfig,但你的问题应该是关于使用 Spring 配置 AspectJ,我对此无能为力:(。
    猜你喜欢
    • 2014-07-30
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2013-10-17
    • 2023-03-24
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多