Em Ae
Em Ae

Reputation: 8724

Unable to autowire repo bean

I am using SpringBoot 1.4.0.RELEASE. My project structure tree looks like this

com
└── projectname
    ├── Application.java
    ├── api
    │   ├── authentication
    │   │   ├── AuthenticatedUser.java
    │   │   ├── AuthenticationRequest.java
    │   │   ├── AuthenticationToken.java
    │   │   ├── AuthenticationTokenFilter.java
    │   │   ├── AuthenticationTokenUtil.java
    │   │   ├── JwtAuthenticationEntryPoint.java
    │   │   └── PasswordRequest.java
    │   ├── common
    │   │   └── CommonClasses.java
    │   ├── config
    │   │   ├── DbConfiguration.java
    │   │   ├── SimpleCORSFilter.java
    │   │   └── WebSecurityConfig.java
    │   └── service
    │       └── AuthenticationService.java
    └── persistence
        ├── domain
        │   └── User.java
        ├── repository
        │   └── UserRepo.java
        └── service
            └── UserService.java

Here is how i have configured my Application.java class for Spring

Application.java

@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableSwagger2
@Log
public class Application extends SpringBootServletInitializer {
    private static Class<Application> app = Application.class;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(app, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(app);
    }
    //other methods to configure swagger and security
 }

Here is how UserRepo looks like

public interface UserRepo extends MongoRepository<User, String> {
    User findById(String emailId);

    List<User> findAll();

}

and thats how i have implemented my UserService.java

@Service
public class UserService {
    @Autowired
    private UserRepo userRepo;
    // methods to fetch data
    }

Now, when I am trying to use userService in authenticationService for login, application is throwing exception that it couldn't find userRepo bean. What I am missing here, here is the exception

14:31:32,930 ERROR [org.springframework.boot.SpringApplication] (ServerService Thread Pool -- 9) Application startup failed: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'userDetailsService': Error creating bean with name 'authenticationService': Unsatisfied dependency expressed through field 'userService': Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepo': No qualifying bean of type [com.projectname.persistence.repository.UserRepo] found for dependency [com.projectname.persistence.repository.UserRepo]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.projectname.persistence.repository.UserRepo] found for dependency [com.projectname.persistence.repository.UserRepo]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepo': No qualifying bean of type [com.projectname.persistence.repository.UserRepo] found for dependency [com.projectname.persistence.repository.UserRepo]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.projectname.persistence.repository.UserRepo] found for dependency [com.projectname.persistence.repository.UserRepo]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticationService': Unsatisfied dependency expressed through field 'userService': Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepo': No qualifying bean of type [com.projectname.persistence.repository.UserRepo] found for dependency [com.projectname.persistence.repository.UserRepo]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.projectname.persistence.repository.UserRepo] found for dependency [com.projectname.persistence.repository.UserRepo]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepo': No qualifying bean of type [com.projectname.persistence.repository.UserRepo] found for dependency [com.projectname.persistence.repository.UserRepo]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.projectname.persistence.repository.UserRepo] found for dependency [com.projectname.persistence.repository.UserRepo]: 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.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:535)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
    at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:150)
    at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:130)
    at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:184)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)

Upvotes: 1

Views: 1086

Answers (1)

Alexander Yanyshin
Alexander Yanyshin

Reputation: 1370

You probably forgot to add Spring Data MongoDb configuration or using wrong basePackages, something like this:

@Configuration
@EnableMongoRepositories(basePackages = "com.projectname.persistence")
class ApplicationConfig extends AbstractMongoConfiguration {
//...
}

Upvotes: 3

Related Questions