国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Spring獲取管理對象常用方法詳解

瀏覽:5日期:2023-08-07 08:50:50

網上方法很多種,我說一些J2EE開發中會用到的方法。

第一種:

直接初始化Spring容器,獲得對象

ApplicationContext applicationContext = new ClassPathXmlApplicationContext('applicationContext.xml'); applicationContext.getBean('beanId');

關于配置文件的讀取也有好多種,我用到的是配置文件在SRC下面。

這樣會初始化Spring容器,然后再得到配置的對象。

第二種:

通過環境來獲得

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext()); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); ac1.getBean('beanId'); ac2.getBean('beanId');

區別是前者會拋異常,而后者沒有時返回NULL

第三種:

實現ApplicationContextAware接口

下面給出實現類,這也是我用的方法

import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * @說明 獲得Spring配置中的某個對象 * @author 崔素強 * @see */ public class SpringFactory implements ApplicationContextAware { private static ApplicationContext context; @SuppressWarnings('static-access') @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } public static Object getObject(String id) { Object object = null; object = context.getBean(id); return object; } }

這是WEB開發中可以用到的集中方法,當然還有其他方法,歡迎大家積極提供!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 宿松县| 霍林郭勒市| 临沂市| 军事| 三原县| 团风县| 雷山县| 阿拉尔市| 卢氏县| 平和县| 阿坝县| 永胜县| 株洲市| 建阳市| 墨玉县| 红河县| 罗源县| 绥德县| 清远市| 巴里| 安西县| 庆城县| 灵丘县| 罗甸县| 阳新县| 武陟县| 吴堡县| 连南| 泰来县| 唐河县| 高雄市| 文安县| 化隆| 大连市| 台南县| 安陆市| 洪雅县| 且末县| 三门县| 南平市| 砀山县|