Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we get rid of bean definition for Aspects using Spring-AOP

My Aspect is as follows:

@Aspect
public class SomeAspect{
    //define pointcuts
    // define Advices pertaining to pointcuts
}

My aspect-config xml file:

<?xml ...?>
  <beans ...
   xmlns:aop
   xmlns:context ..>
      <bean id="someAspect" class="...SomeAspect"/>
      <aop:aspectj-autoproxy />

  </beans>

This runs perfectly fine

What I require:

I want to get rid of writing bean definition for each Aspect as shown above in my config xml file.

I tried the following:

Added @Component on SomeAspect and in xml added <context-component-scan> with respective package containing my Aspect hoping that my class gets picked up as a Bean and as an Aspect. However, my Aspect was not getting picked up at all.

Any pointers?

like image 275
Vikram Avatar asked Oct 05 '22 11:10

Vikram


1 Answers

The context:component-scan element has a sub-element which you can use to include annotation classes to use to be picked up by the scan.

Take a look at the context:include-filter element, and its attributes.

<context:component-scan base-package="my.package.to.scan">
    <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>

I would think what you tried would have work, but without seeing it exactly as you did it, it is hard to say for sure.

like image 111
nicholas.hauschild Avatar answered Oct 10 '22 03:10

nicholas.hauschild