简体   繁体   中英

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. I want it to remain a POJO. If I were to use xml spring configuration, I could do it easily:

<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. 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.

You need to make another class BlaBlaAopConfig or something like that which will be your configuration class.

Java config is not different from xml, you need to define it and you should do it seperate from your code. Only difference you get, while using java config over xml is better (or worse depending on personal preferences) clarity. But you never want to mix aop and pojo code in one class.

You will define in @Pointcut your target class BlaBla

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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