2015-02-24 95 views
0
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daaSController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ca.mock.dass.storage.mockservice.StorageService com.ca.mock.daas.storage.controller.DaaSController.storageService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ca.mock.dass.storage.mockservice.StorageService] 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), @org.springframework.beans.factory.annotation.Qualifier(value=storageService)} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186) 
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:302) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109) 
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) 
at com.ca.mock.daas.storage.controller.Application.main(Application.java:15) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ca.mock.dass.storage.mockservice.StorageService com.ca.mock.daas.storage.controller.DaaSController.storageService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ca.mock.dass.storage.mockservice.StorageService] 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), @org.springframework.beans.factory.annotation.Qualifier(value=storageService)} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:522) 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298) 
... 16 common frames omitted 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ca.mock.dass.storage.mockservice.StorageService] 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), @org.springframework.beans.factory.annotation.Qualifier(value=storageService)} 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494) 
... 18 common frames omitted 

代碼與此相關的錯誤:春天開機使用@autowire註釋拋出 'BeanCreationException'

Application.java

package com.ca.mock.daas.storage.controller; 

import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.SpringApplication; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

import com.ca.mock.dass.storage.mockservice.StorageServiceMockImpl; 

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

DaaSController.java

package com.ca.mock.daas.storage.controller; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

import com.ca.mock.dass.storage.mockservice.StorageService; 


@RestController 
public class DaaSController { 

    private static final String V1_DAAS = "/v1/daas"; 
    private static final String APPLICATION_JSON_CHARSET_UTF_8 = "application/json; charset=utf-8"; 



    @Autowired 
    @Qualifier("storageService") 
    StorageService storageService; 


    @RequestMapping(value = V1_DAAS, produces = APPLICATION_JSON_CHARSET_UTF_8) 
    public String cecs() { 
     return storageService.cecs(); 
    } 
} 

StorageService.java

package com.ca.mock.dass.storage.mockservice; 


public interface StorageService { 


    String cecs(); 

} 

StorageServiceImpl.java

package com.ca.mock.dass.storage.mockservice; 


public class StorageServiceMockImpl implements StorageService { 


    @Override 
    public String cecs() { 
     return DassStorageUtil.mockDasdWithMetrics("Cecs"); 
    } 

DassStorageUtil具有代碼從文件中讀取並返回一個字符串。

應用程序的context.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
     http://www.springframework.org/schema/task 
     http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 

     <context:component-scan base-package="com.ca.mock.daas.storage.controller"/> 
     <context:annotation-config /> 
     <bean id="daaSController" class="com.ca.mock.daas.storage.controller.DaaSController"/> 
     <bean id="storageService" class="com.ca.mock.dass.storage.mockservice.StorageServiceMockImpl"/> 

</beans> 

是否有人可以幫助我這個錯誤。

+0

爲什麼您發佈'應用context.xml'?你的代碼似乎沒有使用它。 – 2015-02-24 16:26:46

+0

如何連接這個,因爲我在application-context.xml中定義了我的bean – user2613399 2015-02-24 16:30:22

+0

可能是因爲我在「application-context.xml」中定義了「NoSuchBeanDefinitionException」的原因 – user2613399 2015-02-24 16:31:39

回答

1

如果你打算使用XML配置,你需要導入資源

@ComponentScan 
@EnableAutoConfiguration 
@ImportResource("application-context.xml") 
public class Application { 
    public static void main(final String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 
+0

是否有任何特定的位置來保存Maven項目結構中的這個XML文件? – user2613399 2015-02-24 16:37:20

+0

@ user2613399我在回答中提出的方式預計該文件位於應用程序類路徑的根目錄下。如果你想把它放在別的地方,在其他一些軟件包中,你需要修改'String',''/com/example/application-context.xml'。在maven項目中,它會轉到相應的'/ src/main/resources'。 – 2015-02-24 16:39:23

+0

儘管我把它放在src/main/resources中,但我得到了下面的錯誤:線程「main」中的異常org.springframework.beans.factory.BeanDefinitionStoreException:IOException從類路徑資源[application-context.xml]解析XML文檔;嵌套異常是java.io.FileNotFoundException:無法打開類路徑資源[application-context.xml],因爲它不存在 \t at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) – user2613399 2015-02-24 16:41:52