无法自动装配repo bean

时间:2016-12-22 19:45:49

标签: java spring spring-boot spring-data autowired

我正在使用SpringBoot 1.4.0.RELEASE。我的项目结构树看起来像这样

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

以下是我为Application.java

配置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
 }

以下是 UserRepo 的外观

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

    List<User> findAll();

}

这就是我如何实现我的 UserService.java

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

现在,当我尝试在userService中使用authenticationService进行登录时,应用程序会抛出异常,因为它无法找到userRepo bean。我在这里缺少的是这里的例外

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)

1 个答案:

答案 0 :(得分:3)

您可能忘记添加Spring Data MongoDb配置或使用错误的basePackages,如下所示:

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