1
votes

Have an Aspect class (@Aspect) which aspect methods are not running. This is because the target object (org.services.myService) is not in the component-scan list. servlet-context.xml:

<context:component-scan base-package="org.controllers" />
<aop:aspectj-autoproxy />

If add the package "org.services" then the pointcut @Pointcut("within(org.services.myService") works.

Issue is can´t change component-scan in the servlet-context.xml:

The project has a separate applicationContext.xml to scan the services folder and there it has a component-scan.

Adding aspectj-autoproxy after each component-scan seems not to work as only finds beans(controllers) scanned in first component-scan

Also, the project will throw an error if I do a component-scan twice on the same folder (JBoss: 2 beans found for autowiring),and other reasons due of the project structure (mixture of XML to allow it running in JBoss and Jetty).

How can I make myService class available to the aspect class without updating context:component-scan ?

2

2 Answers

0
votes

With spring-aop you can only apply advice to spring beans, so if you don't register your myService bean with spring, the advice will not be applied. The advice will only be applied to bean instances you obtain from the spring context.

If you can't change the component scan directive to include the package of that bean, you can still add a configuration class in one of the scanned packages to instantiate your myService and register it as a spring bean.

package org.controllers;

@Configuration
public class MyServiceConfig {
    @Bean
    public MyService myService() {
        return new MyService();
    }
}

Or with xml configuration into one of your spring xml configs:

<bean class="org.services.MyService" />
0
votes

Each XML configuration file (application context and servlet context) needs to create the beans on which the aspect is created, and also the Aspect class. I was missing the latest one in the component-scan:

<context:component-scan base-package="org.services, org.aspects"/> 
<aop:aspectj-autoproxy />

Each XML that processes the aspect (aspectj-autoproxy) needs to have in the same scope all beans required. (in this case, created through component-scan).