不實(shí)用spring讀取ApplicationContet.xml文件中的數(shù)據(jù)源配置
在web應(yīng)用沒有啟用之前我們需要對數(shù)據(jù)庫進(jìn)行操作,此事spring沒有加載完成,我們依然可以使用jdbc對數(shù)據(jù)庫進(jìn)行操作。
實(shí)現(xiàn)方法如下
配置一個(gè)struts監(jiān)聽器,
實(shí)現(xiàn)ServletContextListener接口
public class HuaweiDBListener implements ServletContextListener {
}
實(shí)現(xiàn)接口中方法
public void contextInitialized(ServletContextEvent context) {
String serverPath = context.getServletContext().getRealPath("/");
ApplicationContext appC= new FileSystemXmlApplicationContext(serverPath + "WEB-INF\\spring\\applicationContext.xml");
ComboPooledDataSource c = (ComboPooledDataSource) appC.getBean("dataSource");
System.out.println("DriverClass=="+ c.getDriverClass());
}
使用ComboPooledDataSource對象直接從ApplicationContet.xml中得到數(shù)據(jù)源配置。
使用jdbc操作數(shù)據(jù)庫就不列舉了。
實(shí)現(xiàn)方法如下
配置一個(gè)struts監(jiān)聽器,
實(shí)現(xiàn)ServletContextListener接口
public class HuaweiDBListener implements ServletContextListener {
}
實(shí)現(xiàn)接口中方法
public void contextInitialized(ServletContextEvent context) {
String serverPath = context.getServletContext().getRealPath("/");
ApplicationContext appC= new FileSystemXmlApplicationContext(serverPath + "WEB-INF\\spring\\applicationContext.xml");
ComboPooledDataSource c = (ComboPooledDataSource) appC.getBean("dataSource");
System.out.println("DriverClass=="+ c.getDriverClass());
}
使用ComboPooledDataSource對象直接從ApplicationContet.xml中得到數(shù)據(jù)源配置。
使用jdbc操作數(shù)據(jù)庫就不列舉了。