通過ApplicationContextAware載入Spring上下文環境

來源:互聯網
上載者:User

標籤:err   檔案中   www   tar   cat   get   title   imp   使用方法   

通過ApplicationContextAware載入Spring上下文環境原創 2013年09月05日 18:15:44
  • 標籤:
  • Spring /
  • ApplicationContextAw
 

項目用到了ApplicationContextAware,通過它Spring容器會自動把上下文環境對象調用ApplicationContextAware介面中的setApplicationContext方法。

我們在ApplicationContextAware的實作類別中,就可以通過這個上下文環境對象得到Spring容器中的Bean。

使用方法如下:

1.實現ApplicationContextAware介面:

[java] view plain copy
  1. package com.bis.majian.practice.module.spring.util;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.ApplicationContextAware;  
  6.   
  7. public class SpringContextHelper implements ApplicationContextAware {  
  8.     private static ApplicationContext context = null;  
  9.   
  10.     @Override  
  11.     public void setApplicationContext(ApplicationContext applicationContext)  
  12.             throws BeansException {  
  13.         context = applicationContext;  
  14.     }  
  15.       
  16.     public static Object getBean(String name){  
  17.         return context.getBean(name);  
  18.     }  
  19.       
  20. }  


2.在Spring的設定檔中配置這個類,Spring容器會在載入完Spring容器後把內容物件調用這個對象中的setApplicationContext方法:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.     http://www.springframework.org/schema/tx   
  8.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
  9.     http://www.springframework.org/schema/context   
  10.     http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">  
  11.       
  12.     <bean id="springContextHelper" class="com.bis.majian.practice.module.spring.util.SpringContextHelper"></bean>  
  13.       
  14.     <context:component-scan base-package="com.bis.majian.practice.module.*" />  
  15. </beans>  


3.在web項目中的web.xml中配置載入Spring容器的Listener:

[html] view plain copy
  1. <!-- 初始化Spring容器,讓Spring容器隨Web應用的啟動而自動啟動 -->  
  2.     <listener>  
  3.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.     </listener>  


4.在項目中即可通過這個SpringContextHelper調用getBean()方法得到Spring容器中的對象了。

通過ApplicationContextAware載入Spring上下文環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.