2015-10-16 132 views
0

我使用Spring引導和maven創建示例應用程序。使用@Autowire的自動接線不工作。我得到以下異常:Spring Boot - Bean創建異常

[2015-10-16 16:39:51.233] boot - 3216 INFO [main] --- AnnotationConfigApplicationContext: Refreshing org.spring[email protected]5f0fd5a0: startup date [Fri Oct 16 16:39:51 IST 2015]; root of context hierarchy 
[2015-10-16 16:39:51.921] boot - 3216 WARN [main] --- AnnotationConfigApplicationContext: Exception encountered during context initialization - cancelling refresh attempt 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testApp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bean.TestBean com.test.TestApp.testBean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bean.TestBean] 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.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) [spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE] 
at com.test.TestApp.main(TestApp.java:22) [classes/:?] 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bean.TestBean com.test.TestApp.testBean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bean.TestBean] 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:561) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
... 15 more 

TestApp.java

@SpringBootApplication 
@EnableAutoConfiguration 
public class TestApp { 

@Autowired 
private TestBean testBean; 

public static void main(String[] args) { 
    ApplicationContext context = SpringApplication.run(TestApp.class, args); 
    context.getBean(TestApp.class); 
    } 
} 

TestBean.java

@Component 
public class TestBean { 

@PostConstruct 
public void init() { 
    System.out.println("init from TestBean"); 
} 
private String name; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
    } 
} 

誰能幫助我?

+1

您可以發佈您的Spring配置(XML或Java)?看起來像你的'TestBean'類在Spring中是未知的:'找不到[com.bean.TestBean]類型的限定bean'。你的課程包是什麼? –

+0

我無法在我的設置中重現這一點,你有沒有在xml中配置引導? – Saravana

+0

我沒有使用任何xml配置文件。我只使用spring引導和maven –

回答

2

默認@SpringBootApplication僅對應用程序類的包和所有子包啓用組件掃描。由於TestApp的包是com.test,並且TestBean的包是com.bean,所以組件掃描不會檢測到這個bean類。

解決方案:
如果您在使用Spring引導1.2.x的:

以下任一註釋添加到您的TestApp類:

@ComponentScan({"com.test", "com.bean"}) 

或移動中,testBean到的子包COM .test,例如com.test.bean

如果您已經使用了Spring啓動的1.3.x:

以下參數添加到您的@SpringBootApplication註釋:

@SpringBootApplication(scanBasePackages = {"com.test", "com.bean"})  
+0

我將類TestBean移到了package com.test中,它工作正常! –

0

春天似乎並不知道你的TestBean所以@Autowired無法在bean註冊表中找到它。

爲了使@Autowired工作在@EnableAutoConfiguration註釋後添加@ComponentScan

如果您在your-app-context.xml嘗試定義TestBean添加此

@ImportResource(value={"your-app-context.xml"}) 

@EnableAutoConfiguration後的註釋。 因此,舉例來說,如果你的應用程序內-context.xml的是位於到Eclipse項目的src/main /資源的META-INF/spring文件夾,正確的路線是:

@ImportResource(value={"/META-INF/spring/your-app-context.xml"}) 

它將將在該上下文中聲明的所有bean暴露給Spring Boot。

+0

我沒有使用任何xml配置文件。我正在使用彈簧引導。 –

+0

更新回答:嘗試添加@ComponentScan – abarisone

+0

'@ SpringBootApplication'已經包含'@ ComponentScan'。 – dunni

相關問題