2

I started with generating my application using JHipster v.3.5.1.

After some time, I needed to create validator to perform some business logic validation on my entity, when it is created with POST. So I made:

@Component
public class MyValidator implements Validator

Then, I tried to inject it into my controller (annotated with @RestController), but no matter which way I tried, it always resulted in something like that:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.my.app.service.domain.MyValidator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

Ways I tried to create bean and inject it

@Autowired
private MyValidator myValidator;

@Inject
private MyValidator myValidator;

@Autowired
@Qualifier("myValidator")
private MyValidator myValidator; (with @Component("myValidator") on class)

@Inject
@Qualifier("myValidator")
private MyValidator myValidator; (with @Component("myValidator") on class)

//Below was inserted in class annotated with @Configuration
@Bean
public MyValidator myValidator() {
   return new MyValidator();
}

However I tried it - it failed. I always got NoSuchBeanDefinitionException or field value was set to null.

I've also checked class location in project structure. To be 100% percent sure it's well placed, I've put it in package the with @Services, which are scanned and work well. No effect.

I know that it seems to be pretty easy task and I know this injection is possible (I've seen it done in project in my work), but somehow I'm not able to make it work in my project.

Maybe I'm missing something in configuration? Thanks for any help :)

1 Answers1

0

I believe your issue is that when you use @Autowired inside a class annotated with @Configuration you are just referencing to a bean that is defined in a separate configuration file, that is it has to be declared in another file also with the @Configuration annotation.

If you want to refer refer to another implicit bean such as your validator annotated with @Component you will need to do it in another implicit bean also annotated to with implicit notation such as @Component, @Service, @Controller, etc

The @Autowired alone should work unless you have more than one class implementing the same interface. That is when you will need to use the @Qualifier.

megalucio
  • 5,051
  • 2
  • 22
  • 26
  • You got it wrong :) I'm trying to use @Autowired/@Inject/@Whatever inside class annotated with @RestController. I've made a small edit to make it more clear. – Alt-Rock Ninja Sep 15 '16 at 20:18
  • Pardon me, all right, I understand, I will see if I can find a better answer. – megalucio Sep 16 '16 at 15:00
  • Could this answer be of any help? http://stackoverflow.com/a/23615478/370209 Or, an even more complete example with code-only configuration: https://teamtreehouse.com/library/displaying-validation-messages – DmytroL Jan 29 '17 at 16:16
  • Can you help me solve this https://stackoverflow.com/questions/62376233/valid-not-working-spring-boot-when-method-is-called-from-same-class – ammy Jun 15 '20 at 17:30