spring hibernate listener 簡單配置
一、springXML配置檔案
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- <property name="dataSource"> <ref bean="dataSource" /> </property> --> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> <context:component-scan base-package="com.enjoy.traffic.deal"></context:component-scan>
</beans>
二、hibernate.cfg.xml配置檔案
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- oracle驅動 --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- JDBC URL --> <property name="connection.url">jdbc:oracle:thin:@192.168.10.224:1521:MCSJT</property> <!-- 資料庫使用者名稱--> <property name="connection.username">MCS_JT</property> <!-- 資料庫密碼--> <property name="connection.password">MCS_JT</property> <!-- 設定是否顯示SQL語句--> <property name="show_sql">true</property> <!-- 設定是否格式化SQL語句 --> <property name="format_sql">true</property> <!-- 設定使用執行緒--> <property name="current_session_context_class">thread</property> <!-- <mapping resource="hibernate/vo/user.xml"/> --> </session-factory> </hibernate-configuration>
三,web監聽
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class ServerStart implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub }
@Override public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("start server ..."); WebApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()); WaterDeal waterDeal = (WaterDeal)application.getBean("waterDeal"); waterDeal.cad(); }
} 四、hibernate動態匯入