`
pupi
  • 浏览: 439614 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

解决spring问题(Unsatisfied dependency)的意外发现

阅读更多
前几天在做一个Dao的单元测试的时候,碰到了一个spring的错误。如下:

引用
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)


我的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="uUIDClassDao" class="my.demo.dao.hibernate.UUIDClassDaoHibernate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </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。

分享到:
评论
8 楼 二十一 2007-08-12  
很有用,支持楼主
7 楼 打倒小日本 2007-06-20  
以前也遇到过这种问题,总算知道是为什么了,谢谢楼主!
6 楼 pupi 2007-06-19  
downpour 写道
连续两个帖子发现这种JavaBean的规范问题,还套了那么大一个标题。

难道不会搜索一下吗?


拜托看清楚帖子再说话。

我拿unsatisfied dependency在论坛中搜了,没有相近的问题。
问题的关键是,我一开始并不清楚这问题和javabean规范有关。

而且并不是所有javaeyer都像你那么牛,每个规范都那么熟。

发这个帖子,只是希望今后有朋友碰到相近的问题,用 Unsatified dependency搜索,还能发现原来是javabean规范导致的问题。

如果大家觉得没有帮助,可以把这个帖子投成隐藏帖。让其只存放在blog中。
5 楼 downpour 2007-06-19  
连续两个帖子发现这种JavaBean的规范问题,还套了那么大一个标题。

难道不会搜索一下吗?
4 楼 pupi 2007-06-19  
galaxystar 写道
注意下规范就行了,很多框架都是默认的按照首字母大写来做的!


之前确实不知道java会把一个mRef的field,识别成MRef的属性。
3 楼 pupi 2007-06-19  
decapitalize是一个静态方法,将一个字符串的首字母从大写变成小写。
getBeanInfo调用了几次这个方法,但是好像和我说的不完全是一件事情呢。下面是这个方法的说明。

Utility method to take a string and convert it to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone.

Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".

2 楼 galaxystar 2007-06-19  
注意下规范就行了,很多框架都是默认的按照首字母大写来做的!
1 楼 joachimz 2007-06-19  
这是javabean规范的一部分,用于处理缩写等情况。建议可以去看看java.beans.Introspector decapitalize的源代码

相关推荐

    spring boot整合mybatis-plus启动报错问题及解答.pdf

    错误信息通常类似于"Error starting ApplicationContext",并提示"UnsatisfiedDependencyException",这表明Spring框架在初始化bean时遇到了未满足的依赖。在这种情况下,错误通常与Mapper接口的自动扫描和配置有关...

    25号05: Spring MVC1课堂内容

    Spring MVC 是一个基于Java的轻量级Web应用框架,它为构建模型-视图-控制器(Model-View-Controller,MVC)结构的Web应用程序提供了强大的支持。在"25号05:Spring MVC1课堂内容"中,我们可以推测这是一个教学资源,...

    javaweb各种框架组合案例(四):maven+spring+spr….docx

    - `org.springframework.beans.factory.UnsatisfiedDependencyException`: 这表明Spring容器在尝试创建`userServiceImpl`时未能满足其依赖关系。 - `org.springframework.beans.factory.BeanCreationException`: ...

    基于springboot+mybatis实现的外卖订餐系统源码+项目说明(毕设).zip

    - org.springframework.beans.factory.UnsatisfiedDependencyException 不满足依赖异常 - org.springframework.beans.factory.BeanCreationException - org.springframework.beans.BeanInstantiationException - org...

    JPA爬坑.pdf

    接下来,我们遇到了另一个问题,即在创建bean时出现异常,具体是“Unsatisfied dependency expressed through field 'userService'”。这意味着Spring无法满足依赖注入,因为依赖的`userService`没有被正确地初始化...

    SPRING API 2.0.CHM

    All Classes AbstractAdvisorAutoProxyCreator AbstractApplicationContext AbstractApplicationEventMulticaster AbstractAspectJAdvice AbstractAspectJAdvisorFactory AbstractAspectJAdvisorFactory....

    Java框架知识点学习笔记

    最后,如果在运行时出现“UnsatisfiedDependencyException”,表示Spring无法满足依赖注入的要求。这通常是因为bean的构造参数类型与配置文件中的声明不一致。例如,配置文件中可能有如下错误代码: ```xml 杨凯" /&gt;...

    SpringBoot 整合 JMSTemplate的示例代码

    在高版本 SpringBoot(2.0 以上)中,添加 activemq 连接池依赖项时可能会出现 Error creating bean with name 'xxx': Unsatisfied dependency expressed through field 'jmsTemplate' 的错误,可以将 activemq 连接...

Global site tag (gtag.js) - Google Analytics