Autowiring :expected at least 1 bean which qualifies as autowire candidate for this dependency

Viewed 57783

Okay, I know that there are many questions asked around the same topic. But I cant seem to make anything work. It also might be the case that I am yet to completely understand the auto wiring concept. My Problem: I am able to get to the required page, but whenever I click on the any button to perform an action I get Null pointer exception which seems obvious as I dont think spring is able to properly map the bean needed.

So, when I add @autowired=true , it gives me the above given exceptionn. I am not sure what needs to be done.. Hope someone can help me out with this. Would love an explanation as well:) Code:

@Entity
@Table(name="userDetails")
public class UserDetailModel {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int user_id;
public String password;
public String user_name;
public String active_status;

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getUser_name() {
    return user_name;
}

public void setUser_name(String user_name) {
    this.user_name = user_name;
}

public int getUser_id() {
    return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

public String getActive_status() {
    return active_status;
}

public void setActive_status(String active_status) {
    this.active_status = active_status;
}

}

Controller:

@RestController
public class UserDetailController {

 private Logger logger = (Logger) LoggerFactory.getLogger(UserDetailController.class);
 @Autowired(required = true)
private UserRepository userRepository;

@RequestMapping(value="/login", method = RequestMethod.POST)
public @ResponseBody String addNewUser (@RequestBody UserDetailModel user) {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    logger.debug("in controller");
    UserDetailModel userDtl = new UserDetailModel();
    userDtl.setUser_id(user.user_id);
    userDtl.setUser_name(user.user_name);
    userDtl.setActive_status(user.active_status);
    userDtl.setPassword(user.password);

    userRepository.save(userDtl);
    return "Saved";
}

}

Repository:

@Repository
public interface UserRepository extends CrudRepository<UserDetailModel,         Long> {}

Stack Trace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springBoot.usl.repo.UserRepository com.springBoot.usl.controller.UserDetailController.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springBoot.usl.repo.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:683)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
at com.springBoot.usl.controller.WebAppInitializer.main(WebAppInitializer.java:18)

Solved based on the responses Answer: I made some modifications based on Jay and Luay's answers. And changed the annotations as follows in my ApplicationConfig file:

@Configuration
@ComponentScan("my.basepackage.*")
@EnableJpaRepositories(basePackages = {"my.basepackage.*"})
@EntityScan("my.basepackage.*")
@EnableAutoConfiguration

Hope this helps some one.

But I am not sure if * is the right way to go.

6 Answers

I know that the problem was solved, but as it can be problem for someone else like I had, I decided to share my problem and the solution. I got exactly the same error message, but the problem was the lack of the annotation @Service in a class and hence an object of this type couldn't autowire. After putting this annotation, everything worked fine.

Below is the object of the mentioned class:

@Autowired
private JwtUserDetailsService jwtUserDetailsService;

Just this annotation caused the same problem and can pass unnoticed:

@Service  // annotation that was missing
public class JwtUserDetailsService implements UserDetailsService { ...

We had a similar problem in a kotlin + spring boot application and could solve it thanks to this solution slightly modified:

@SpringBootApplication
@ComponentScan("package.path*")
Related