java - 无法 Autowiring repo bean

标签 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

下面是我如何为 Spring 配置我的 Application.java

应用程序.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
    }

现在,当我尝试在 authenticationService 中使用 userService 进行登录时,应用程序抛出异常,它找不到 userRepo bean .我在这里缺少的是异常(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)

最佳答案

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

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

关于java - 无法 Autowiring repo bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41290626/

相关文章:

java - 在 Ubuntu 15.04 上从 NetBeans 8.0.2 卸载或禁用 JRebel

java - 使用 Java 在 Mac 上以编程方式发送电子邮件(通过 Mac 邮件客户端)

java - 类 ClassFormatError :Absent-Code-attribute-in-method-that-is-not-native

java - Spring 框架集合被驱逐

spring - 如何自定义 Hibernate @Size 错误消息以指示输入字段的长度

java - Spring Boot REST验证错误响应

java - 我如何知道选择哪个 Maven 依赖版本?

java - Jetty servlet 对 Ajax 的响应总是空的

java - Spring Integration 服务激活器引用 Hibernate JPA 存储库方法

java - 如何在 Spring Boot RestController 中获取请求 URL