Spring transaction practice, an error example:
package lin;
import lin.dao.impl.ExchangeDaoLocator;
import lin.dao.impl.TestDaoLocator;
import lin.domain.Exchange;
import lin.domain.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class DbTest {
public static void main(String[] args) {
try {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
jdbcTemplate.execute("select * from dual");
ExchangeDaoLocator locator1 = (ExchangeDaoLocator) applicationContext.getBean("exchangeDaoLocator");
Exchange exchange = locator1.findById("HK");
System.out.println(exchange.getExchangeCode());
TestDaoLocator locator2 = (TestDaoLocator) applicationContext.getBean("testDaoLocator");
Test test = locator2.findById("1");
System.out.println(test.getC2());
System.out.println("----------------End----------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}
<bean id="abstractDaoLocator" class="lin.dao.AbstractDaoLocator" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="exchangeDaoLocator" class="lin.dao.impl.ExchangeDaoLocator" parent="abstractDaoLocator"> </bean> <bean id="testDaoLocator" class="lin.dao.impl.TestDaoLocator" parent="abstractDaoLocator"> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> <property name="defaultTimeout" value="15"/> <property name="rollbackOnCommitFailure" value="true"/> </bean> <aop:config> <!-- Normal Example --> <aop:advisor pointcut="execution(* lin.DbTest..*(..))" advice-ref="requiresNewTxAdvice" /> <!-- Wrong Example - java.lang.ClassCastException: $Proxy8 incompatible with lin.dao.impl.ExchangeDaoLocator --> <aop:advisor pointcut="execution(* lin.dao.impl.*.findById(..))" advice-ref="requiresNewTxAdvice" /> </aop:config> <tx:advice id="requiresNewTxAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRES_NEW" /> </tx:attributes> </tx:advice>

本文通过一个Spring事务实践的实例,详细介绍了如何在Java应用程序中使用Spring管理事务,并阐述了一个具体的错误案例,深入分析了Java.lang.ClassCastException错误产生的原因及解决方法。
5万+

被折叠的 条评论
为什么被折叠?



