2015-02-24 88 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