我知道關於此問題的許多類似問題,但是,它們中沒有一個解決了我的問題。我有一個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
.....
存儲庫必須位於main(Application)類的相同包或子包中。這是Spring啓動時的默認行爲。 – 2014-11-25 11:32:30
謝謝@Evgeni。實際上,我之前已經有了這個設置,並且它正在工作。但是將存儲庫移到自己的包中以保持乾淨。與主班級保持一致的邏輯是什麼? – Sami 2014-11-25 11:35:52
爲了保持乾淨,您還可以將repos放在一個子包中,比如'com.example.rest.repo'。 – 2014-11-25 11:39:09