Eduardo Cintra
Eduardo Cintra

Reputation: 101

How inject a Spring-boot service into a aspectj class?

I'm with a problem.. I'm creating a aspectj class and into my class I need to access a spring-boot service, but when I try use @Autowired or inject it through a constructor I have an error:

"Could not autowire. No beans of 'UserService' type found"

Here my class:

package com.ingressolive.bar.aop.interceptor;


@Aspect
@Configuration
public class TenantAspect {
    private final Logger log = LoggerFactory.getLogger(this.getClass());


    private final Environment env;

    @Autowired
    private UserService userService;


    public TenantAspect(Environment env) {
        this.env = env;

    }

    @Before("execution(* com.ingressolive.bar.service.*.*(..))")
    public void aroundExecution(JoinPoint pjp) {
        log.debug("##################################### Entered here !!!!!!!!!!!!!!!!!!!!!!!!!!");

    }
}

Can someone help me?

Upvotes: 3

Views: 2270

Answers (1)

mrkurtan
mrkurtan

Reputation: 591

Could you try with Component instead of Configuration? I am using aspects like this, and autowiring works perfectly.

package com.ingressolive.bar.aop.interceptor;

@Aspect
@Component
public class TenantAspect {
   ...
}

Maybe you have to look for other configuration issues e.g. profiles, not loaded xml configs? If you have any xml config for your beans, consider using the following pattern:

package com.yourpackage.config;

@Configuration
@ImportResource(
        locations = {
                "classpath:/your-extra-config-1.xml",
                "classpath:/your-extra-config-2.xml",
        })
public class AppConfig {
    ...
}

Upvotes: 2

Related Questions