1.<!-- Hibernate配置 -->
2.<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
3.<property name="dataSource" ref="dataSource" />
4.<property name="namingStrategy">
5.<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
6.</property>
7.<property name="hibernateProperties">
8.<props>
9.<prop key="hibernate.dialect">${hibernate.dialect}</prop>
10.<prop key="hhibernate.autoReconnect">${hibernate.autoReconnect}</prop>
11.<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
12.<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
13.<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
14.<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
15.
16.//之前获取hibernate session 空指针 在配置里加了这2个属性就OK了 但是 dao查询时候必须要开启事务,sessionFactory.getCurrSession() 获取当前session
17.
18.<prop key="hibernate.current_session_context_class">thread</prop>
19.<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
20.</props>
21.</property>
22.<property name="packagesToScan">
23.<list>
24. <value>com.unionpay.portal</value>
25. </list>
26.</property>
27.</bean>
<!-- 服务器启动定时器 首先要在 web.xml 监听-->
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ApplicationListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent arg0) {
//new 出一个时间探测器
Timer t=new Timer();
//每隔一定时间执行任务
t.schedule(new MyTask(), 0, 30*1000);
}
}
MyTask 为自定义类。继承timer? schedule第一个参数类型
//============================================================
//以上配置有误,这样配置会出现,定时器查询session 和 正常启动页面session 冲突。导致页面500 获取不到session而定时器则可以获取。
//修改为 将以上2 配置属性去除, 改为this.sessionFactory.open() , session.close();手动开始和关闭session
dao 类 继承 HibernateDao