2014-11-25 377 views
1

我知道关于此问题的许多类似问题,但是,它们中没有一个解决了我的问题。我有一个Spring REST项目,我正在使用Spring Tool Suite(STS)3.5.1 RELEASE。@ComponentScan和@Autowired无法从特定程序包中注入

应用程序类:

package com.example.rest; 

@ComponentScan({"com.example.repositories", "com.example.config", "com.example.services", "com.example.rest", "com.example.jms"}) 
@EnableAutoConfiguration 
public class Application 
{ 
    ... //declaring some beans for JMS 
} 

repository类:

package com.example.repositories; 

@Repository 
public interface ActorRepository extends MongoRepository<Actor, String> 
{ 

    public Actor findByFNameAndLName(String fName, String lName); 
    public Actor findByFName (String fName); 
    public Actor findByLName(String lName); 

} 

的服务类(自动装配这里没有注入actorRepository):

package com.example.services; 

@Service 
public class ActorService 
{ 
    @Autowired 
    private ActorRepository actorRepository; 
    .... 
} 

REST服务(自动装配Autowired无法注入actorService -I假设它是因为ActorService无法注入ActorRe pository):

package com.example.rest; 

@RestController 
@RequestMapping("/actors") 

public class ActorRESTService 
{ 
    private static final Logger logger = Logger.getLogger(ActorRESTService.class); 

    @Autowired 
    private ActorService actorService; 

    .... 
} 

我相信它正在发生,因为@ComponentScan不扫描库包的原因是,在STS,Spring类有thier的Java图标的右上角一个小S。这出现在所有应该扫描的类上(除了存储库软件包中的任何部分外)。将存储库类移动到其余的软件包使其扫描(不知道为什么!)。

这是我在尝试使用Spring Boot App运行项目时所获得的偏移的一部分。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'actorService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repositories.ActorRepository com.example.services.ActorService.actorRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repositories.ActorRepository] 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.example.rest.Application.main(Application.java:94) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repositories.ActorRepository com.example.services.ActorService.actorRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repositories.ActorRepository] 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:508) 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
... 16 common frames omitted 

..... 
+0

存储库必须位于main(Application)类的相同包或子包中。这是Spring启动时的默认行为。 – 2014-11-25 11:32:30

+0

谢谢@Evgeni。实际上,我之前已经有了这个设置,并且它正在工作。但是将存储库移到自己的包中以保持干净。与主班级保持一致的逻辑是什么? – Sami 2014-11-25 11:35:52

+1

为了保持干净,您还可以将repos放在一个子包中,比如'com.example.rest.repo'。 – 2014-11-25 11:39:09

回答

2

存储库必须位于main(Application)类的相同packaege或子包中。这是Spring启动时的默认行为。为了保持干净,您也可以将回购商品放在一个小包装中,例如com.example.rest.repo。或者当M. Deinum拥塞时,您可以将主类放在基本包中,以便Spring可以自动处理该类。