迷信Spring SpringBeanHibernateDAOORM
程序员文章站
2022-07-10 22:34:24
...
说来也怪,我使用相同的程序代码来做应用,因为基本模板已经定义,所以所有的工作几乎就是COPY和PASTE,但是就是这样做,也发现了一个Spring的一个漏洞。如下解说:
原来的注入XML
----------------------------------------------------------------------
..................................
<bean id="mssqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="mssqlObjectDao" class="com.throuhout.j2eemodel.spring.service.dao.hibernate.MssqlHibernateDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- Service -->
<!-- mssqlObjManager -->
<bean id="mssqlObjectManagerTarget" class="com.throuhout.j2eemodel.spring.service.MssqlManagerImpl">
<property name="mssqlObjectDao">
<ref local="mssqlObjectDao" />
</property>
</bean>
<bean id="mssqlObjectManager" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="mssqltransactionManager" />
</property>
<property name="target">
<ref local="mssqlObjectManagerTarget" />
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
------------------------------------------------------------------
JAVA文件。
------------------------------------------------------------------
public void setMssqlObjectDao(IMssqlObjectDao mssqlObjectDao) {
dao = mssqlObjectDao;
}
------------------------------------------------------------------
以上的不能够成功运行,在依赖注入生成BEAN时发生错误,为什么呀?
就是这个关键字mssqlObjectDao、mssqlObjectManager、mssqlObjectManagerTarget的错误,因为你的Dao前面的字符有大小写的缘故,就测试不能够通过。
呵呵,不瞞大家说,我在JAVAEYE上提出这个问题,还被ROBBIN删除了,自力更生,呀呀,终于达到解决。
如下是正确的代码。
--------------------------------------------------------------------
<bean id="mysqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mysqlSessionFactory" />
</property>
</bean>
<bean id="mssqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mssqlSessionFactory" />
</property>
</bean>
<bean id="userDao" class="com.throuhout.j2eemodel.spring.service.dao.hibernate.UserHibernateDao">
<property name="sessionFactory">
<ref local="mysqlSessionFactory" />
</property>
</bean>
<bean id="mssqlDao" class="com.throuhout.j2eemodel.spring.service.dao.hibernate.MssqlHibernateDao">
<property name="sessionFactory">
<ref local="mssqlSessionFactory" />
</property>
</bean>
<!-- Service -->
<!-- userManager -->
<bean id="userManagerTarget" class="com.throuhout.j2eemodel.spring.service.UserManagerImpl">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>
<bean id="userManager" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="mysqltransactionManager" />
</property>
<property name="target">
<ref local="userManagerTarget" />
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- mssqlObjManager -->
<bean id="mssqlManagerTarget" class="com.throuhout.j2eemodel.spring.service.MssqlManagerImpl">
<property name="mssqlDao">
<ref local="mssqlDao" />
</property>
</bean>
<bean id="mssqlManager" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="mssqltransactionManager" />
</property>
<property name="target">
<ref local="mssqlManagerTarget" />
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
-------------------------------------------------------------------
public void setMssqlDao(IMssqlObjectDao mssqlDao) {
dao = mssqlDao;
}
-----------------------------------------------------------------
欢迎大家互相交流,互相学习。
原来的注入XML
----------------------------------------------------------------------
..................................
<bean id="mssqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="mssqlObjectDao" class="com.throuhout.j2eemodel.spring.service.dao.hibernate.MssqlHibernateDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- Service -->
<!-- mssqlObjManager -->
<bean id="mssqlObjectManagerTarget" class="com.throuhout.j2eemodel.spring.service.MssqlManagerImpl">
<property name="mssqlObjectDao">
<ref local="mssqlObjectDao" />
</property>
</bean>
<bean id="mssqlObjectManager" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="mssqltransactionManager" />
</property>
<property name="target">
<ref local="mssqlObjectManagerTarget" />
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
------------------------------------------------------------------
JAVA文件。
------------------------------------------------------------------
public void setMssqlObjectDao(IMssqlObjectDao mssqlObjectDao) {
dao = mssqlObjectDao;
}
------------------------------------------------------------------
以上的不能够成功运行,在依赖注入生成BEAN时发生错误,为什么呀?
就是这个关键字mssqlObjectDao、mssqlObjectManager、mssqlObjectManagerTarget的错误,因为你的Dao前面的字符有大小写的缘故,就测试不能够通过。
呵呵,不瞞大家说,我在JAVAEYE上提出这个问题,还被ROBBIN删除了,自力更生,呀呀,终于达到解决。
如下是正确的代码。
--------------------------------------------------------------------
<bean id="mysqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mysqlSessionFactory" />
</property>
</bean>
<bean id="mssqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mssqlSessionFactory" />
</property>
</bean>
<bean id="userDao" class="com.throuhout.j2eemodel.spring.service.dao.hibernate.UserHibernateDao">
<property name="sessionFactory">
<ref local="mysqlSessionFactory" />
</property>
</bean>
<bean id="mssqlDao" class="com.throuhout.j2eemodel.spring.service.dao.hibernate.MssqlHibernateDao">
<property name="sessionFactory">
<ref local="mssqlSessionFactory" />
</property>
</bean>
<!-- Service -->
<!-- userManager -->
<bean id="userManagerTarget" class="com.throuhout.j2eemodel.spring.service.UserManagerImpl">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>
<bean id="userManager" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="mysqltransactionManager" />
</property>
<property name="target">
<ref local="userManagerTarget" />
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- mssqlObjManager -->
<bean id="mssqlManagerTarget" class="com.throuhout.j2eemodel.spring.service.MssqlManagerImpl">
<property name="mssqlDao">
<ref local="mssqlDao" />
</property>
</bean>
<bean id="mssqlManager" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="mssqltransactionManager" />
</property>
<property name="target">
<ref local="mssqlManagerTarget" />
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
-------------------------------------------------------------------
public void setMssqlDao(IMssqlObjectDao mssqlDao) {
dao = mssqlDao;
}
-----------------------------------------------------------------
欢迎大家互相交流,互相学习。
推荐阅读
-
Spring MVC温故而知新系列教程之请求映射RequestMapping注解
-
Spring实例化bean过程解析及完整代码示例
-
Spring Security使用中Preflight请求和跨域问题详解
-
使用spring工厂读取property配置文件示例代码
-
Spring工厂方法创建(实例化)bean实例代码
-
spring Boot 应用通过Docker 来实现构建、运行、发布流程
-
Spring用代码来读取properties文件实例解析
-
spring boot linux启动方式详解
-
spring Boot打包部署到远程服务器的tomcat中
-
spring boot启动时mybatis报循环依赖的错误(推荐)