简体   繁体   English

如何仅使用Java配置在spring config中定义方面

[英]How to define an aspect inside spring config using java configuration only

Here is an example of how you can define an aspect using annotations: 这是一个如何使用注释定义方面的示例:

@Aspect
public class BlaBla() {
    @Pointcut(...)
    ...

    @Before(...)
    ...

    @After(...)
    ...
}

This is the only way that I found through research. 这是我通过研究发现的唯一方法。 However I want class BlaBla to not be polluted with annotations and pointcut methods. 但是我希望类BlaBla不会被注解和切入点方法污染。 I want it to remain a POJO. 我希望它保持POJO。 If I were to use xml spring configuration, I could do it easily: 如果我要使用xml spring配置,则可以轻松实现:

<bean id="blaBla" class="BlaBla">...</bean>

<aop:config>
    <aop:aspect ref="blaBla">
        <aop:pointcut .../>
        <aop:before .../>
        <aop:after .../>
    </aop:aspect>
</aop:config>

However, I am not using xml configuration, but a Java configuration instead. 但是,我不是使用xml配置,而是使用Java配置。 Basically question is, how can I say to spring that class BlaBla is an aspect without changing BlaBla class itself, but instead to define it inside a java class annotated with @Configuration spring annotation. 基本上,问题是,如何说出类BlaBla是一个方面,而又不更改BlaBla类本身,而是在带有@Configuration spring注释的java类中定义它。

You need to make another class BlaBlaAopConfig or something like that which will be your configuration class. 您需要制作另一个类BlaBlaAopConfig或类似的东西作为您的配置类。

Java config is not different from xml, you need to define it and you should do it seperate from your code. Java配置与xml并无不同,您需要对其进行定义,并且应该将其与代码分开。 Only difference you get, while using java config over xml is better (or worse depending on personal preferences) clarity. 在xml上使用java config时,获得的唯一区别是更好(或更糟,具体取决于个人喜好)。 But you never want to mix aop and pojo code in one class. 但是,您永远不想在一个类中混合使用aop和pojo代码。

You will define in @Pointcut your target class BlaBla 您将在@Pointcut中定义目标类BlaBla

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM