spring容器初始化的几种方式

整理了几种常见的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();
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值