2 u011682543 u011682543 于 2016.01.14 16:21 提问

spring-test junit4出现Could not autowire field问题 10C

不知道为什么自动注入失败?帮忙看看什么原因?拜谢~~
1. spring配置文件

<context:property-placeholder
    ignore-unresolvable="true" location="classpath:config.properties" />

<!-- 自动扫描(自动注入) -->
<context:component-scan base-package="com.epuxun.eshuixiang">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 引入其他配置文件 -->
<import resource="classpath:spring-context-ehcache.xml" />
<import resource="classpath:spring-context-mybatis.xml" />
<import resource="classpath:spring-context-captcha.xml" />
<import resource="classpath:spring-context-jedis.xml" />
<import resource="classpath:spring-context-task.xml" />

2.@service 注入(这里不贴出所有代码了,太长,主要看@service)
@Service("accountService")
@Transactional(readOnly = true)
public class AccountServiceImpl implements IAccountService {

private Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

private Logger logger = Logger.getLogger(getClass());

@Autowired
private AccountMapper accountMapper;

3.测试类使用spring-test+junit4
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-context*.xml"})
public class TestMyDm{
private static final Logger logger = Logger.getLogger(TestMyDm.class);

@Autowired
private IAccountService accountService;

@Test
public void test1() {
    Account account = accountService.findByMemberId("ba175fbea5684e809e46d46ca886e18a");
    logger.info(JSON.toJSON(account));
}

}

4.报错代码
[org.springframework.context.support.GenericApplicationContext]Refreshing org.springframework.context.support.GenericApplicationContext@65fbadc2: startup date [Thu Jan 14 16:03:58 CST 2016]; root of context hierarchy
[org.springframework.test.context.TestContextManager]Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@25a57416] to prepare test instance [com.epuxun.eshuixiang.TestMyDm@116863a9]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.epuxun.eshuixiang.TestMyDm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.epuxun.eshuixiang.modules.account.service.IAccountService com.epuxun.eshuixiang.TestMyDm.accountService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.epuxun.eshuixiang.modules.account.service.IAccountService] 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:301)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:384)
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:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
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:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
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.BeanCreationException: Could not autowire field: private com.epuxun.eshuixiang.modules.account.service.IAccountService com.epuxun.eshuixiang.TestMyDm.accountService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.epuxun.eshuixiang.modules.account.service.IAccountService] 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:522)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.epuxun.eshuixiang.modules.account.service.IAccountService] 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.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)
... 28 more
[org.springframework.context.support.GenericApplicationContext]Closing org.springframework.context.support.GenericApplicationContext@65fbadc2: startup date [Thu Jan 14 16:03:58 CST 2016]; root of context hierarchy

2个回答

devmiao
devmiao   Ds   Rxr 2016.01.19 04:41
CSDNXIAOD
CSDNXIAOD   2016.01.14 16:23
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
[Spring] Could not autowire field 异常的一种可能原因
出现这种异常情况,有可能是你当前类注入到当前类引起的 比如如下代码 @Service(value="flowNodeService") @Transactional public class FlowNodeServiceImpl extends BaseServiceImpl<FlowNode, Integer> implements IFlowNodeService{ @Autowired
could not autowire field 错误解决方式
这个错误的解决方式 package com.rc.mst.dao; import java.util.HashMap; import java.util.List; import com.rc.it.cppt.core.mybatis.BaseMyBatisDao; import com.rc.it.cppt.core.mybatis.MyBatisDao; impor
mybatis错误org.springframework.beans.factory.BeanCreationException: Could not autowire field
这个问题真的是整的头都大了。 整了 2 3天,还是幸好别人帮忙解决了, 毕竟也是自己忘性大 再加上,不够细心吧,眼力 差了点一直在网络上 查 springmvc 报这样的错误的原因本来就觉得代码是没有问题。 本来代码好好的。 删除了1个数据源的配置就报错了, 但是添加上一个数据源 就不报错了,一期: 服务器项目,是需要链接2个数据源 来完成功能的。 二期: 合并成一个数据源了。修改
[SpringMVC] Could not autowire field 异常的一种可能原因
SpringMVC 项目启动服务器时报错Could not autowire field ... 反复确认这个接口以及实现类都存在,最后终于发现在实现类上忘加@Service注解了。 启发来自以下这篇博客: autowire异常的三个情况
springmvc整合mybatis出现Could not autowire field:No matching bean of type错误
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.us.service.UsersService 。。。。。 Caused by: org.springframework.beans.factory.NoSuchBeanDefiniti
@Autowired:【Could not autowire field:】
================提高效率的宝扫描 方式:指定具体包名。 =====带来的问题:新添加需要自动注入属性时,常常忘记 添加包扫描。 导致 报错:【Could not autowire field:】 ================
Could not autowire field: private com.xxx.dao(已解决)
最近刚在做一个关于o2o在线资源回收的一个项目,用到的框架就是SSM框架,可能有一段时间没有写代码了,一些常见的错误都折腾了半天,直接进入正题: 这个图片就是当时报错的图片。 当时是在控制器里注解接口的时候,运行程序直接就报错 @Autowired private UserDao userdao; 去网上搜了一些错误,但是跟我的不太一样,所以尝试了没有什么结果。后来,在查看...
Could not autowire field: private java.lang.Integer com.taotao.sso.service.impl.UserServiceImpl.SSO_
@Value("REDIS_USER_SESSION_KEY") private String REDIS_USER_SESSION_KEY;改正:@Value("${SSO_SESSION_EXPIRE}") private Integer SSO_SESSION_EXPIRE;
关于SpringMVC中org.springframework.beans.factory.BeanCreationException: Could not autowire field:的问题的解决
在idea中编写Spring MVC+mybatis项目时报下面错误: 代码上提示的错误为: 经排查发现是dao层注解没有写,导致无法织入。 总结:遇到上面的错误时大致有三种可能情况: 第一、xml配置文件不对。 第二、注解没加 第三 、包冲突
could not autowire field 错误解决方式
1.你的BrandServiceImpl必须以@Service或@Component注解才行。 2.自动写入的时候把接口写成实现类了 @Autowired private BrandServiceImpl      brandServiceImpl; 应该是 @Autowired private BrandService    brandService ; 3.在BrandDao 类上加上@Rep...