整理了几种常见的spring容器初始化方式,如下:
1、web工程启动的时候初始化:
使用监听类,web.xml中配置如下:
<!-- 指定spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- 监听类 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
监听类中需要用到log4j包,故在加载spring之前需要先加载log4j,配置如下:
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/lib/log4j.properties</param-value> </context-param> <!-- ContextLoaderListener类初始化spring时需要用到log4j,故log4j需要在初始化spring之前进行初始化 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
完整配置如下:
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/lib/log4j.properties</param-value> </context-param> <!-- 指定spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- listener --> <!-- ContextLoaderListener类初始化spring时需要用到log4j,故log4j需要在初始化spring之前进行初始化 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 监听类 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
启动初始化之后,在Java代码中获取WebApplicationContext对象,代码如下:
public void init(ServletConfig config) throws ServletException {
//启动的时候已经装载了spring,这里直接获取wac对象
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
DateContext dc = (DateContext)wac.getBean("dateContext");
JSONObject json = new JSONObject();
System.out.println("测试spring:" + dc.get("monthLastDay", json));
}
2、需要用到的时候才初始化:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.panda.TestSpring;
public class Test {
public static void main(String[] args) {
// BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
// TestSpring ts = (TestSpring) bf.getBean("testSpring");
// ts.print();
// Resource resource = new ClassPathResource("applicationContext.xml");
// BeanFactory factory = new XmlBeanFactory(resource);
//一、 用classpath路径
// ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
//同时加载多个配置文件
// String[] configFile = {"applicationContext.xml","applicationContext.xml1"};
// ApplicationContext factory = new ClassPathXmlApplicationContext(configFile);
// ClassPathXmlApplicationContext使用了file前缀是可以使用绝对路径的
// ApplicationContext factory = new ClassPathXmlApplicationContext("file:E:/learning_demo/TestSpring/src/applicationContext.xml");
//二、 用文件系统的路径,默认指项目的根路径
// ApplicationContext factory = new FileSystemXmlApplicationContext("src/applicationContext.xml");
// 使用了classpath:前缀,这样,FileSystemXmlApplicationContext也能够读取classpath下的相对路径
// ApplicationContext factory = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
// ApplicationContext factory = new FileSystemXmlApplicationContext("file:E:/learning_demo/TestSpring/src/applicationContext.xml");
// 不加file前缀
ApplicationContext factory = new FileSystemXmlApplicationContext("E:/learning_demo/TestSpring/src/applicationContext.xml");
TestSpring ts = (TestSpring)factory.getBean("testSpring");
ts.print();
}
}