如何獲得spring上下文的方法總結(jié)
一 前言
打算重溫spring,以后可能每周會(huì)發(fā)一篇吧,有空就搞搞;
二 獲取上下文的幾種方式
AnnotationConfigApplicationContext:從一個(gè)或多個(gè)基于Java的配置類中加載Spring應(yīng)用上下文。 AnnotationConfigWebApplicationContext:從一個(gè)或多個(gè)基于Java的配置類中加載Spring Web應(yīng)用上下文。 ClassPathXmlApplicationContext:從類路徑下的一個(gè)或多個(gè)XML配置文件中加載上下文定義。 FileSystemXmlapplicationcontext:從文件系統(tǒng)下的一個(gè)或多個(gè)XML配置文件中加載上下文定義。 XmlWebApplicationContext:從Web應(yīng)用下的一個(gè)或多個(gè)XML配置文件中加載上下文定義2.1 準(zhǔn)備工作
被單實(shí)體
public class Sheet { // 顏色 private String color; // 長度 private String length; // 省略 set get}
sheet.xml 里面注入了Bean Sheet, 并且默認(rèn)初始化 color值為red;
<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd'> <bean class='com.zszxz.bean.Sheet'> <property name='color' value='pink'></property> </bean></beans>
2.2FileSystemXmlapplicationcontext 獲取上下文
FileSystemXmlApplicationContext 構(gòu)造器參數(shù)中需要指定sheet.xml具體文件系統(tǒng)路徑;獲得上下文之后再通過getBean方法獲取Bean Sheet; 拿到對(duì)象后使用getColor 方法打印顏色,為pink;
public static void main(String[] args) { // xml路徑 String path = 'C:javaworkspaceforresourcestudy-springobtain-bean-waysrcmainresourcessheet.xml'; // 從文件系統(tǒng)中獲取上下文 ApplicationContext applicationContext = new FileSystemXmlApplicationContext(path); // 獲取bean Sheet sheet = (Sheet) applicationContext.getBean('sheet'); // pink System.out.println(sheet.getColor()); }
2.3ClassPathXmlApplicationContext獲取上下文
ClassPathXmlApplicationContext 傳入?yún)?shù)是類路徑下sheet.xml的路徑;
public static void main(String[] args) { // 獲取上下文 ApplicationContext applicationContext = new ClassPathXmlApplicationContext('sheet.xml'); // 獲得實(shí)例 Sheet sheet = (Sheet) applicationContext.getBean('sheet'); // pink System.out.println(sheet.getColor()); }
2.4AnnotationConfigApplicationContext獲取上下文
AnnotationConfigApplicationContext 獲取上下文,是通過java配置的方式獲取上下文;知識(shí)追尋者這邊需要進(jìn)行java配置,內(nèi)容如下,等同于之前的sheet.xml
/** * @Author lsc * <p> sheet配置類等同于sheet.xml</p> */@Configurationpublic class SeetConfig { // 往配置類中注入Bean @Bean public Sheet sheet(){ // 創(chuàng)建對(duì)象 Sheet sheet = new Sheet(); // 設(shè)置屬性 sheet.setColor('pink'); return sheet; }}
獲取方式如下,傳入AnnotationConfigApplicationContext 參數(shù)是SeetConfig.class
public static void main(String[] args) { // 獲取上下文 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SeetConfig.class); // 獲得實(shí)例 Sheet sheet = (Sheet) applicationContext.getBean('sheet'); // pink System.out.println(sheet.getColor()); }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP中實(shí)現(xiàn)字符部位類似.NET里String對(duì)象的PadLeft和PadRight函數(shù)2. vue前端RSA加密java后端解密的方法實(shí)現(xiàn)3. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能4. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)5. vue項(xiàng)目登錄成功拿到令牌跳轉(zhuǎn)失敗401無登錄信息的解決6. 淺談?dòng)蓀osition屬性引申的css進(jìn)階討論7. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享8. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)9. 基于jsp+mysql實(shí)現(xiàn)在線水果銷售商城系統(tǒng)10. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享

網(wǎng)公網(wǎng)安備