<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"
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/spring-context-2.5.xsd
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"\>
<context:annotation-config />
<context:component-scan base-package="com.spring" />
<bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/spring/model/user.hbm.xml</value>
<value>com/spring/model/userlog.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<tx:advice transaction-manager="transactionManager" id="txManager">
<tx:method name="save"/>
</tx:attributes>
</tx:advice>
<aop:pointcut id="entryPointMethod" expression="execution(public * com.spring.service..*.*(..))"/>
<aop:advisor
advice-ref="txManager"
pointcut-ref="entryPointMethod"
/>
</aop:config>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
2、使用@resource 注入,然后调用save方法:
[java] view plain copy
@Component("userDaoImpl")
public class UserDaoImpl implements UserDao{
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(User u) {
try {
hibernateTemplate.save(u);
} catch (HibernateException e) {
e.printStackTrace();
}
}
}