一直以为ref和idref没有大的区别,它们两个可以替换。
网上也有很多人认为这两个没区别。
最近做Spring和Hibernate的集成的时候发现配置sessionFactory的dataSource的时候用idref不行,用ref就行。
配置文件beans.xml为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>org/li/hibernate/spring/entity</value>
</list>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>jdbc:mysql://localhost:3306/spring</value>
</property>
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>java</value>
</property>
</bean>
<bean id="ArticleDAO" class="org.li.spring.hibernate.dao.implement.ArticleDAO">
<property name="hibernateTemplate">
<ref local="hibernateTemplate"/>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
如果将上面的
<property name="dataSource">
<ref local="dataSource"/>
</property>
换成
<property name="dataSource">
<idref local="dataSource"/>
</property>
就出错了
这是我的测试类:
Resource res=new ClassPathResource("beans.xml");
BeanFactory bf=new XmlBeanFactory(res);
ArticleDAO articleDao = (ArticleDAO) bf.getBean("ArticleDAO");
Article article = new Article();
article.setTitle("学习Spring");
article.setContent("Spring和Hibernate的如何集成的呢~");
article.setAuthor("javafish");
articleDao.save(article);
异常发生在org.springframework.beans.TypeConverterDelegate这个类中的
convertIfNecessary函数里的
if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) {
// Definitely doesn't match: throw IllegalArgumentException.
throw new IllegalArgumentException("No matching editors or conversion strategy found");
两句代码上:
分别用ref和idref配置beans.xml调试了程序
发现如果是用idref配置的话,当解析到dataSource的时候requiredType的值为"java.sql.dataSource,
cov,ertedValue的值为"dataSource"<----这只是个在配置文件里标识类的字符串不是我们想要的类
如果是用ref配置的话,当解析到dataSource的时候requiredType的值为"java.sql.dataSource,cov,ertedValue的值为"DriverManagerDataSource"<-----这正是我们想要的类
所以idref有两个作用,1.方便xml检查,2.它和<value>差不多,只一个字符串。
ref呢:是spring提供的用于引用bean的元素。
网上也有很多人认为这两个没区别。
最近做Spring和Hibernate的集成的时候发现配置sessionFactory的dataSource的时候用idref不行,用ref就行。
配置文件beans.xml为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>org/li/hibernate/spring/entity</value>
</list>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>jdbc:mysql://localhost:3306/spring</value>
</property>
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>java</value>
</property>
</bean>
<bean id="ArticleDAO" class="org.li.spring.hibernate.dao.implement.ArticleDAO">
<property name="hibernateTemplate">
<ref local="hibernateTemplate"/>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
如果将上面的
<property name="dataSource">
<ref local="dataSource"/>
</property>
换成
<property name="dataSource">
<idref local="dataSource"/>
</property>
就出错了
这是我的测试类:
Resource res=new ClassPathResource("beans.xml");
BeanFactory bf=new XmlBeanFactory(res);
ArticleDAO articleDao = (ArticleDAO) bf.getBean("ArticleDAO");
Article article = new Article();
article.setTitle("学习Spring");
article.setContent("Spring和Hibernate的如何集成的呢~");
article.setAuthor("javafish");
articleDao.save(article);
异常发生在org.springframework.beans.TypeConverterDelegate这个类中的
convertIfNecessary函数里的
if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) {
// Definitely doesn't match: throw IllegalArgumentException.
throw new IllegalArgumentException("No matching editors or conversion strategy found");
两句代码上:
分别用ref和idref配置beans.xml调试了程序
发现如果是用idref配置的话,当解析到dataSource的时候requiredType的值为"java.sql.dataSource,
cov,ertedValue的值为"dataSource"<----这只是个在配置文件里标识类的字符串不是我们想要的类
如果是用ref配置的话,当解析到dataSource的时候requiredType的值为"java.sql.dataSource,cov,ertedValue的值为"DriverManagerDataSource"<-----这正是我们想要的类
所以idref有两个作用,1.方便xml检查,2.它和<value>差不多,只一个字符串。
ref呢:是spring提供的用于引用bean的元素。