3

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?

Eduardo Cintra
  • 101
  • 1
  • 11
  • Do you have any instances of UserService registered as beans? Seems like you dont. – Shadov Jul 26 '19 at 22:17
  • Why are you using both `@Aspect` and `@Configuration` on the same class? Anyway, the error message is very specific. – chrylis -cautiouslyoptimistic- Jul 26 '19 at 22:50
  • if you remove the aspect part and make the class good old pure java do you get the same error ? – Dr Phil Jul 26 '19 at 22:52
  • @DrPhil About the configuration I thought that in this way it was correct. The UserService is a beans, I used it in other places of the code through of annotation autowired. When I remove the two annotations there is not more erros. – Eduardo Cintra Jul 27 '19 at 02:12

1 Answers1

2

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 {
    ...
}
mrkurtan
  • 573
  • 2
  • 13
  • After that I posted here, A friend at work told me to try with annotation "component" and remove the annotation "configuration". It work, but not very well, if I declare a method that return just a String in the UserService, works! But, if I have a mtehod that use a Repository for example, I get many Exceptions. I'm not at my computer now, tomowrrow I will post here the erro. – Eduardo Cintra Jul 27 '19 at 02:19
  • Please post the error. Sounds like it could be that the DAO and Service is mixed up in your setup. You should use Dao in your service injected (autowired), and the Dao implementation should be annotated with Repository. And use interfaces all the time, not the concrete class to be able to proxy them via interface. – mrkurtan Jul 27 '19 at 02:28
  • 2
    I don't know why, I was testing now and it just worked. Using as you said, without annotation "configuration" and adding annotation "component". Thank you so much! – Eduardo Cintra Jul 29 '19 at 15:16