日期:2014-05-18 浏览次数:20700 次
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u = (User)ac.getBean("userServiceImpl");
u.setId("101");
u.setPwd("101");
UserServiceImpl userServiceImpl = new UserServiceImpl();
userServiceImpl.addUser(u);
}
}
applicationContext.xml<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- applicationContext.xml事务管理器的配置 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<!-- 注入applicationContext.xml中配置的sessionFactory -->
<ref bean="sessionFactory" />
</property>
</bean>
<!-- dao -->
<bean id="UserDaoImpl" class="com.wr.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- service -->
<bean id="userServiceImpl" class="com.wr.service.impl.UserServiceImpl">
<property name="userDao" ref="UserDaoImpl"></property>
</bean>
<!-- action -->
<bean name="addUserAction" class="com.wr.action.addUserAction">
<property name="userServiceImpl" ref="userServiceImpl"></property>
</bean>
<!-- 事务通知的配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 对do开头的方法要求事务 -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<!-- 对其他方法的配置 -->
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:a