spring-注解

提示一下错误信息为提示响应使用的注解类未被注入

2016-01-13 14:20:57,602 ERROR [org.springframework.test.context.TestContextManager:309] - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@20fedcfd] to prepare test instance [com.qinghuainvest.cmndd.DictServiceTest@3c5e76f7]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.qinghuainvest.cmndd.DictServiceTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.qinghuainvest.base.service.YptDictionaryFactoryDB] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:307)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.qinghuainvest.base.service.YptDictionaryFactoryDB] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:440)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:418)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:546)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
    ... 26 more

 看我使用案例

package com.qinghuainvest.cmndd;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;

import com.qinghuainvest.TestBaseCtrl;
import com.qinghuainvest.base.service.YptDictionaryFactoryDB;
import com.qinghuainvest.cmndd.dict.model.Dictionary;

public class DictServiceTest extends TestBaseCtrl {
    @Resource
    private YptDictionaryFactoryDB yptDictionaryFactoryDB;
    @Test
    public void test() {
        Dictionary dict = yptDictionaryFactoryDB.getDictionary("0001");
        System.out.println(dict);
    }
    
    @Test
    @SuppressWarnings("rawtypes")
    public void testFind(){
        List list  = super.ybEnterpriseBasicInfoService.findList();
        System.out.println(list.size());
    }
}

 

我以为如下类应该是已经被注入了

package com.qinghuainvest.base.service;

import org.springframework.stereotype.Component;

import com.qinghuainvest.cmndd.dict.service.impl.DictionaryFactoryDB;
import com.qinghuainvest.cmndd.util.cfg.ConfigFile;

@Component("yptDictionaryFactoryDB")
public class YptDictionaryFactoryDB extends DictionaryFactoryDB {
    private static final long serialVersionUID = 1L;

    public YptDictionaryFactoryDB() {
        .....
    }
}

 

但是时发现被未被注入,提示上面错误。

究其原因在于:spring的配置注解文件类库中并未标识指定文件下的类导致未被识别

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <context:component-scan base-package="com.qinghuainvest.enterprise.*" />
    <context:component-scan base-package="com.qinghuainvest.institution.*" />
    <context:component-scan base-package="com.qinghuainvest.report.*" />
    <context:component-scan base-package="com.qinghuainvest.securities.*" />
    <context:component-scan base-package="com.qinghuainvest.test.*" />
    <context:component-scan base-package="com.qinghuainvest.cmndd.*" />
    <task:annotation-driven />
</beans>

 

所以最后将修改的地方为(下面红色一行,想需要识别的类库引入)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <context:component-scan base-package="com.qinghuainvest.enterprise.*" />
    <context:component-scan base-package="com.qinghuainvest.institution.*" />
    <context:component-scan base-package="com.qinghuainvest.report.*" />
    <context:component-scan base-package="com.qinghuainvest.securities.*" />
    <context:component-scan base-package="com.qinghuainvest.test.*" />
    <context:component-scan base-package="com.qinghuainvest.base.*" />
    <context:component-scan base-package="com.qinghuainvest.cmndd.*" />
    <task:annotation-driven />
</beans>

 

 

执行结果:成功

Dictionary[code=0001,name=企业管理人类型,enumMap={0001-0001=Enumeration[code=0001-0001,name=控股股东,log=org.apache.log4j.Logger@254efcf1], 0001-0002=Enumeration[code=0001-0002,name=实际控制人,log=org.apache.log4j.Logger@254efcf1]},log=org.apache.log4j.Logger@64c04559]

 

posted @ 2016-01-13 14:27  243573295  阅读(1408)  评论(0编辑  收藏  举报