spring讀取配置文件初始化容器操作總結

Spring初始化容器.三種經常用到的實現:

一、ClassPathXmlApplicationContext:從類路徑中加載。

二、FileSystemXmlApplicationContext:從文件系統加載。

三、XmlWebApplicationContext:web系統中加載。

使用1bean工廠:最簡單的容器,提供了基礎的依賴注入支持。創建各種類型的Bean.

BeanFactory factory = null; //聲明

ClassPathResource resource = new ClassPathResource("spring.xml");//類路徑

FileSystemResource fileSystemResource =

new FileSystemResource("D:\\Ncode\\mcode\\sday02\\src\\spring.xml");

//系統路徑

//InputStreamResource res

//= new InputStreamResource(

//newFileInputStream("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"));

factory= newXmlBeanFactory(resource);

//XmlBeanFactory(參數可以是resource或者fileSystemResource等

//但是不能是 res 原因可以查看:文檔Part III. Core Technologies 6. Resources

//中6.2 The Resource interface 有關isOpen方法的說明);

factory = new ClassPathXmlApplicationContext("spring.xml");  

//從類路徑中加載

factory = new

FileSystemXmlApplicationContext("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"); //從文件系統加載

HelloService helloService =

factory.getBean("helloServiceImpl", HelloServiceImpl.class);

helloService.sayHello();

使用2

  應用上下文:建立在bean工廠基礎之上,提供系統架構服務。ApplicationCotext,spring更加高級的容器。功能強大:

1.提供文本信息解析工具,包括對國際化支持。

2.提供載入文件資源的通用方法,如圖片。

3.可以向註冊爲監聽器的bean發送事件。

在很少的情況下,使用BeanFactory,如在移動設備。

ApplicationContext context = new

FileSystemXmlApplicationContext("file:D:\\Ncode\\mcode\\sday02\\src\\spring.xml");

context = new

ClassPathXmlApplicationContext("classpath:spring.xml");

HelloService helloService =

context.getBean("helloServiceImpl", HelloServiceImpl.class);

helloService.sayHello();

使用3、在web應用程序中

1、採用XMLWebApplication對象 初始化容器

XmlWebApplicationContext context = new XmlWebApplicationContext();

//默認的路徑/WEB-INF/applicationContext.xml

//applicationContext.xml文件名稱 可以任意起

//重新設置路徑

//context.setConfigLocations(

       //    new String[] {"/WEB-INF/classes/applicationContext.xml"});

       //設置ServletContext上下下文爲web應用程序的上下文

context.setServletContext(getServletContext());

       //刷新

context.refresh();

//根據id名稱獲取

HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);

//執行helloDao對象的方法

helloDao.sayHello();

因爲XmlWebApplicationContext默認加載的文件和文件名稱爲

/WEB-INF/applicationContext.xml

因此需要在WEB-INF下添加此配置文件。

2、利用 WebApplicationContextUtils工具類

//直接採用getWebApplicationContext(getServletContext()) 獲取context對象

WebApplicationContext  context=

WebApplicationContextUtils.getWebApplicationContext(getServletContext());

//context =

//WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

System.out.println(context);

HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);

helloDao.sayHello();

說明:

  1、當採用getWebApplicationContext(getServletContext())獲取context對象的時候,輸出的context對象爲null  所以在使用

context.getBean("helloDaoImpl", HelloDaoImpl.class);會出現空指針的異常

  2、當採用getRequiredWebApplicationContext(getServletContext());獲取context對象的時候 會出現如下bug

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

Bug解決:不關是12都是因爲沒有在web.xml文件中配置ContextLoaderListener,所以只需要在web.xml文件中加入以下代碼中即可

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章