<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<context:component-scan base-package="com.sl" />
<bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring" />
<property name="username" value="root" />
<property name="password" value="qi" />
</bean>
<bean id="factory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="mydataSource" />
<property name="annotatedClasses">
<list>
<value>com.sl.entity.User</value>
<value>com.sl.entity.Log</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="factory" />
</bean>
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="factory" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
@Repository("s_dao")
public class StudentDao {
private HibernateTemplate template;
public HibernateTemplate getTemplate() {
return template;
}
@Resource
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void save(User user) {
template.save(user);
}
}
@Repository("s_log")
public class StudentLog {
private HibernateTemplate template;
public HibernateTemplate getTemplate() {
return template;
}
@Resource
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void save(Log log) {
template.save(log);
}
}
@Component("sss")
public class StudentService {
private StudentDao s_dao;
private StudentLog s_log;
public StudentDao getS_dao() {
return s_dao;
}
@Resource
public void setS_dao(StudentDao sDao) {
s_dao = sDao;
}
public StudentLog getS_log() {
return s_log;
}
@Resource
public void setS_log(StudentLog sLog) {
s_log = sLog;
}
@Transactional
public void add(User user,Log log) {
System.out.println("begin");
s_dao.save(user);
s_log.save(log);
System.out.println("end");
}
}
public class test {
@Test
public void testOne(){
ApplicationContext appl=new ClassPathXmlApplicationContext("beans.xml");
StudentService ss=(StudentService)appl.getBean("sss");
User user=new User("qing");
Log log=new Log("log:save");
ss.add(user, log);
}
}