我的配置文件:
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="test.sunsc.*" />
<!-- 配置Datasource -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>/conf/comm.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:initialSize="10"
p:maxOpenPreparedStatements="10"
p:maxActive="255"
p:maxIdle="2"
p:maxWait="120000"/>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 将dataSource注入给sessionFactory -->
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>test/sunsc/table/hibernateUser.hbm.xml</value>
</list>
</property>
<!-- 配置Hibernate的一些属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
</bean>
<!-- 声明事务管理器,将我们定义的sessionFactory注入 -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- aop指定好切面,指定切面逻辑为txAdvice -->
<aop:config proxy-target-class="true"/>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(* test.sunsc.dao..*.*(..))" />
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
<!-- 在切面逻辑中指定事务管理器为txManager -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="use*" propagation="REQUIRED"/>
<!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>
代码:
public class HibernateUtil {
private static SessionFactory sf;
private static ApplicationContext ctx;
public static SessionFactory getSessionFactory(){
ctx = new FileSystemXmlApplicationContext("conf/appConfig.xml");
sf = (SessionFactory) ctx.getBean("sessionFactory");
return sf;
}
}
public class UserDaoImpl implements UserDao {
public void save(HibernateUser user) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session s =sf.getCurrentSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
s.close();
}
红色部分,为报错部分,不知道如何解决!