引用
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'my.demo.dao.UUIDClassDaoTest': Unsatisfied dependency expressed through bean property 'UUIDClassDao': Set this property value or disable dependency checking for this bean.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
.checkDependencies(AbstractAutowireCapableBeanFactory.java:1019)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
populateBean(AbstractAutowireCapableBeanFactory.java:839)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:273)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.
injectDependencies(AbstractDependencyInjectionSpringContextTests.java:179)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.
prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:158)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(Abstrac
tSingleSpringContextTests.java:88)
at junit.framework.TestCase.runBare(TestCase.java:128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
.checkDependencies(AbstractAutowireCapableBeanFactory.java:1019)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
populateBean(AbstractAutowireCapableBeanFactory.java:839)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:273)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.
injectDependencies(AbstractDependencyInjectionSpringContextTests.java:179)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.
prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:158)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(Abstrac
tSingleSpringContextTests.java:88)
at junit.framework.TestCase.runBare(TestCase.java:128)
我的Dao单元测试类继承自 AbstractTransactionalDataSourceSpringContextTests
这个类,它可以提供对springContext的支持。
从errorstack中看,好像是Dao中的属性没有设置好。 所以, 我检查了代码,Dao中 定义了属性 uUIDClassDao,并且提供了set方法,如下:
private UUIDClassDao uUIDClassDao = null; public void setUUIDClassDao(UUIDClassDao uUIDClassDao) { this.uUIDClassDao = uUIDClassDao; }
在Spring context文件中,定义了如下的bean。
看起来没问题呀,bean的id和dao中定义的属性都是 uUIDClassDao,通过把autowire模式设置成 AUTOWIRE_BY_NAME ,应该我的dao测试类会被自动注入uUIDClassDao这个bean的。
可是很意外,这并没有发生。What’s the problem ?
我又仔细看了看error stack,忽然发现,提示的是 “Unsatisfied dependency expressed through bean property ' UUIDClassDao':”。注意,不是uUIDClassDao,而是UUIDClassDao。
难道Spring会把uUIDClassDao识别成 UUIDClassDao ?
我跟踪了spring的代码,发现,spring在拿到单元测试dao的时候,用了Introspector.getBeanInfo(Class class0) 去得到对象的属性。的的确确,通过这种方式拿到的属性名是 UUIDClassDao,而不是uUIDClassDao。
为了再次验证,我写了一个简单的测试程序:
import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; public class AppBeanInfo { /** * @param args */ public static void main(String[] args) throws Exception { BeanInfo info = Introspector.getBeanInfo(MyReflection.class); for (PropertyDescriptor pd : info.getPropertyDescriptors()) { System.out.println(pd.getName()); } BeanInfo info2 = Introspector.getBeanInfo(MyReflection2.class); for (PropertyDescriptor pd : info2.getPropertyDescriptors()) { System.out.println(pd.getName()); } } } public class MyReflection { String mRef; public String getMRef() { return mRef; } public void setMRef(String ref) { mRef = ref; } } public class MyReflection2 { String mmRef; public String getMmRef() { return mmRef; } public void setMmRef(String mmRef) { this.mmRef = mmRef; } }
输出结果是
MRef
class
class
mmRef
也就是对mRef这种字段,用自省识别出来的属性名竟然是MRef。
so, 在程序中尽量不要定义 uUIDClassDao这种属性,而用uuidClassDao。因为可能在你不知道的地方,uUIDClassDao已经被识别成了 UUIDClassDao。