personServiceImpl.java
package tohibernate.annotation; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service(value = "personService") public class PersonServiceImpl implements PersonService { @Resource(name = "personDao") private PersonDao personDao; @Transactional(readOnly = false) public void savePerson(Person person) { this.personDao.savePerson(person); } }
personDaoImpl.java
package tohibernate.annotation; import javax.annotation.Resource; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; @Repository(value = "personDao") public class PersonDaoImpl implements PersonDao { @Resource(name = "hibernateTemplate") private HibernateTemplate hibernateTemplate; public void savePerson(Person person) { this.hibernateTemplate.save(person); } }
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:context="http://www.springframework.org/schema/context" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 1、目标类 2、通知 3、进行aop的配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <property name="configLocation"> <value>classpath:tohibernate/annotation/hibernate.cfg.xml</value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <context:component-scan base-package="tohibernate.annotation"></context:component-scan> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
浙公网安备 33010602011771号