Spring中Bean的property,ref引发的小问题
来源:互联网 发布:声鉴图用什么软件做 编辑:程序博客网 时间:2024/06/11 14:35
今天在配置Spring的配置文件applicationContext.xml文件时,一时顺手把配置Mybatis的Bean写成了这个样子
结果在测试mapper的时候抛出了这个异常:
警告 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource': no matching editors or conversion strategy found
简单看了下发现Bean的一个property”dataSource”出现了问题,Spring表示并不知道你想要干啥?
看到这一下子就有点懵,心想我不是给你赋了引用类型”pooledDataSource”了么?怎么还…
忽然我就发现我把ref写成了value.
我们知道value是用来赋值一般类型的,在这里Spring就会把我们的”pooledDataSource”当成一个字符串来处理,而不会去找我们配置的pooledDataSource这个数据源bean.
ref是赋值引用类型的,Spring就会去加载对应的bean,完成sqlSessionFactory这个bean的属相注入.
正确的写法应该是
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis全局配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="dataSource" ref="pooledDataSource"/> <!-- 指定mybatis,mapper文件的位置 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean>
编程是一个很严谨的活儿 : )
阅读全文
0 0
- Spring中Bean的property,ref引发的小问题
- spring bean property ref
- spring 中配置bean的注入是<property>的name与与ref的id名需要一样吗??
- Spring的ref bean和ref local
- Spring中Bean的命名问题及ref和idref之间的区别
- Spring中Bean的命名问题及ref和idref之间的区别
- Spring <property 的 ref 属性建立 bean 之前的引用关系
- [JAVA][Spring]Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
- [JAVA][Spring]Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring配置文件中使用ref local与ref bean的区别.
- spring ref local="" 和 ref bean=""的区别
- Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
- spring .xml中bean下的property属性
- Spring 配置文件中 Bean 的 property 属性使用示例
- Spring配置文件中配置property标签的name和ref的区别
- Spring bean id相同引发的故障
- 匿名函数自执行
- HDU 1203 乘法 01背包
- 算法题:BFS之出门最佳
- n个元素进栈,共有多少种出栈顺序?
- Java的三种代理模式
- Spring中Bean的property,ref引发的小问题
- 使用 Diff 和 Meld 工具比较两个目录的不同处
- tf.nn.sparse_softmax_cross_entropy_with_logits()函数的用法
- Scala III
- Qt+VS2008打包程序 | 显示中文乱码
- Java语言组成基础——需要格外注意的问题
- Python入门<六>
- angularjs定时任务的设置与清除
- 线程的五种状态与线程池的知识点