我在用Junit对一个集成了SSH的项目进行做测试。
但在调用bean时(applicationContext.getBean()时)出现了异常:”No unique bean of type“上网查了一下,说是Spring默认按type加载。所以我在applicationContext.xml里这样写:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans default-autowire="byName"><!-- 很多的import语句,因为有很多的bean的配置文件 -->
<import resource="classpath*:/config/beans/*.xml" />
...但这样做还是不行,报同样的错误。我在单个bean(测试类中用到的bean)上加autowire="byName",也不行请问这样的问题怎么解决?附异常信息:说明:我在bean的配置文件里确实有两个transactionManager——
其一,transactionManager(类型:org.springframework.orm.hibernate3.HibernateTransactionManager),
其二,jdbcTransactionManager(类型:org.springframework.jdbc.datasource.DataSourceTransactionManager)
我想,他们都是org.springframework.transaction.PlatformTransactionManager的子类,而spring又是按类型装载,所以就找到了两个实例。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.dayee.test168.manage.user.web.logic.WebUserLogicTest': Unsatisfied dependency expressed through bean property 'transactionManager': : No unique bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: [transactionManager, jdbcTransactionManager]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: [transactionManager, jdbcTransactionManager]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:179)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:158)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:88)
at junit.framework.TestCase.runBare(TestCase.java:132)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: [transactionManager, jdbcTransactionManager]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:621)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1076)
... 20 more

解决方案 »

  1.   

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type
    下面的例子报同样的错误,你参考下修改方法吧
    @Service 
    @Transactional 
    public class UserService implements BusinessService<User>{ 
        public List<User> findAll() { 
            return userDao.findAll(); 
        }     ... 
    } 但是如果我把红色部分去掉,则运行正常,或者不去掉红色部分,把注入部分改为如下,也可以运行正常,这是为啥呢? 
    @Autowired 
        public void setService(@Qualifier("userService")BusinessService<User> service){ 
            this.userService = service; 
        }
      

  2.   

    使用@Autowired 注释 进行byType注入,如果需要byName注入,增加@Qualifier注释这是我在网上查到的。你的方法是注入自己写的类。但是我的注入是这样的:<bean id="transactionProxyTemplate"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    abstract="true" parent="transactionAttributes">
    <property name="transactionManager" ref="transactionManager" />
    </bean>然后在我写的那个类里面用到parent属性将transactionManager注入<bean id="webUserDao" parent="transactionProxyTemplate" singleton="true">
            <property name="target" ref="webUserDaoImpl" />
        </bean>
    我在applicationContext.xml里设置了default-autowire="byName"为什么不起作用呢?
      

  3.   

    附加说明:我在web(tomcat)下运行是没有问题的。
      

  4.   

    上面的问题解决了。
    (我的测试类)
    public class WebUserLogicTest extends AbstractDependencyInjectionSpringContextTests把它的父类改成上面的就好了。
    我以前用的是AbstractTransactionalDataSourceSpringContextTests
    我也不知道为甚要用这个,好像是在网上看的,说是数据库操作可以自动回滚但是又有个新的问题:getBean取不到值。。
    AbstractDependencyInjectionSpringContextTests有个成员变量applicationContext,我查了一下它的类型是org.springframework.context.support.GenericApplicationContext我这样调用:
    applicationContext.getBean("BEAN_NAME") 取到的竟然是null注:BEAN_NAME 在bean文件肯定是配置了的真是命途多舛啊!!!!
    天啊!!!!!!!!
      

  5.   

    介个 junit 测试好久没搞了....
      

  6.   

    这个貌似是applicationContext.getBean("BEANID")