spring管理下系統載入類

來源:互聯網
上載者:User

 系統載入時執行的類,也是我們將需要載入的參數配置的地方 。必須實現 ServletContextListener

 

package com.chain.store.mutual;</p><p>import javax.servlet.ServletContext;<br />import javax.servlet.ServletContextEvent;<br />import javax.servlet.ServletContextListener;</p><p>import org.springframework.context.ApplicationContext;<br />import org.springframework.web.context.WebApplicationContext;<br />import org.springframework.web.context.support.WebApplicationContextUtils;</p><p>import com.chain.store.bean.SystemParamBean;<br />import com.chain.store.service.impl.SystemParamServicesImpl;</p><p>public class LoadConfiguration implements ServletContextListener {<br />private static final long serialVersionUID = 1L;<br />//private SystemParamServicesImpl sysParamService;<br />private static WebApplicationContext springContext;<br />private SystemParamBean sysBean ;<br />private ServletContext context;</p><p>public LoadConfiguration(){<br />super();<br />}</p><p>public void contextDestroyed(ServletContextEvent sce) {<br />System.out.println("系統停止......");</p><p>}</p><p>@SuppressWarnings("unchecked")<br />public void contextInitialized(ServletContextEvent sce) {<br />// TODO Auto-generated method stub<br />System.out.println("系統啟動載入......");</p><p>springContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());<br />SystemParamServicesImpl sysServices = (SystemParamServicesImpl)springContext.getBean("sysParamService");<br />sysBean = sysServices.listAllParams();</p><p>context = sce.getServletContext();</p><p>context.setAttribute("sysParams", sysBean);<br />}</p><p> public static ApplicationContext getApplicationContext() {<br /> return springContext;<br /> } </p><p>}<br />

 

說明 :

SystemParamBean  放置我需要載入的系統參數

sysParamService  是我在 applicationContext.xml 中配置的從資料庫中得到參數值的bean

 

當然不要忘記在 web.xml 中添加

 

<listener><br /> <listener-class><br /> com.chain.store.mutual.LoadConfiguration<br /> </listener-class><br /> </listener>  

 

在Action 中得到系統參數:

ServletContext context = ServletActionContext.getServletContext();<br />SystemParamBean sysBean = (SystemParamBean)context.getAttribute("sysParams");</p><p>System.out.println("----------系統參數----------");<br />System.out.println("NEWS_SHOWIMG " + sysBean.getNEWS_SHOWIMG());<br />System.out.println("---------------------------");  

 

 

參考文章 :

 

http://hi.baidu.com/29163077/blog/item/b3b77bdd5c1a94ea76c638f7.html

 

http://lichengyezi.javaeye.com/blog/508128

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.