2016-10-18 33 views
0

我在in.orgname.projectname.backend无法自动连接DAO在春季启动

@SpringBootApplication 
public class Application { 

    public static void main(String args[]) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

吾道在in.orgname.projectname.backend.model.dao

我的应用程序
public interface MyUserDao extends CrudRepository<MyUser, String>{ 

    public MyUser findByEmailId(String emailId); 
} 

我的实体位于in.orgname.projectname.backend.model.entity

最后试图访问这个DAO在我的控制器是在in.orgname.projectname.backend.controllers

@RestController 
public class AuthenticationController { 

    @Autowired 
    MyUserDao myUserDao; 

    @RequestMapping(value = "/authenticate", method = RequestMethod.POST) 
    public ResponseEntity<String> authenticate(@RequestParam(value = "authcode") String authCode){ 

     //Some other code 
    } 

} 

我得到这个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: in.orgname.projectname.backend.model.dao.MyUserDao in.orgname.projectname.backend.controllers.AuthenticationController.myUserDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [in.orgname.projectname.backend.model.dao.MyUserDao] 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:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at in.orgname.projectname.backend.Application.main(Application.java:11) [classes/:na] 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: in.orgname.projectname.backend.model.dao.MyUserDao in.orgname.projectname.backend.controllers.AuthenticationController.myUserDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [in.orgname.projectname.backend.model.dao.MyUserDao] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    ... 17 common frames omitted 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [in.orgname.projectname.backend.model.dao.MyUserDao] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    ... 19 common frames omitted 

我已经通过各种渠道在StackOverflow上,但似乎没有解决我的问题。

我该如何处理?

EDIT1:我也尝试过直接在DAO类上使用@Component

EDIT2:这里是git仓库到具有相同的错误项目 - https://github.com/amitection/myuser-poc

+0

'iin.orgname.projectname.backend.controllers.AuthenticationController.myUserDao;'中有一个额外的** i **。不知道它是否与问题 –

+0

我会编辑。这只是因为我更改了软件包名称,因为我无法公开项目名称。 – amitection

+0

我创建了一个新项目,复制了你的代码,发明了我自己的'MyUser'实体,我无法重现你的问题。无论你发布的包是错误的,你是否使用不同的导入(可能'CrudRepository'不是Spring数据的接口?)。如果您无法发布适当的MVCE,恐怕我们无法提供帮助。 – g00glen00b

回答

0

您需要设置扫描<context:component-scan base-package="com.example" />在servlet.xml中做自动引用或在春季启动添加@ComponentScan到应用程序类。

+2

Annotation'@ SpringBootApplication'应该已经执行'@ ComponentScan' –

+2

@CarlosRivero True。当其他软件包不是应用程序类所在的软件包的子软件包时,需要使用软件包名称的明确** ComponentScan **。 – amitection

+0

in.orgname.projectname.backend.model.dao是in.orgname.projectname的子软件包。后端,所以它会自动扫描 –

1

基于示例应用程序,问题是您没有将postgresql库作为依赖项。如果没有,Spring将不会自动配置由postgres支持的Repository bean。

您将要添加:

<dependency> 
     <groupId>org.postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
    </dependency> 

春天开机自动配置只加载豆当它检测到的类路径上的特定类。一旦它在类路径上看到postgresql,它就会尝试生成Repository bean。

+0

我想这完全是一个不同的问题。在这种情况下抛出的异常是不同的。即使我添加依赖关系,问题仍然存在。就像我在问题5中提到的那样,它是一些奇怪的操作系统特定问题。我知道这听起来很愚蠢。当我尝试在Windows中运行时,导致相同的代码完美工作。 – amitection