如何獲得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;
// 長(zhǎng)度
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 id="sheet" class="com.zszxz.bean.Sheet">
<property name="color" value="pink"></property>
</bean>
</beans>
2.2FileSystemXmlapplicationcontext 獲取上下文
FileSystemXmlApplicationContext 構(gòu)造器參數(shù)中需要指定sheet.xml具體文件系統(tǒng)路徑;獲得上下文之后再通過(guò)getBean方法獲取Bean Sheet; 拿到對(duì)象后使用getColor 方法打印顏色,為pink;
public static void main(String[] args) {
// xml路徑
String path = "C:\\java\\workspaceforresource\\study-spring\\obtain-bean-way\\src\\main\\resources\\sheet.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 獲取上下文,是通過(guò)java配置的方式獲取上下文;知識(shí)追尋者這邊需要進(jìn)行java配置,內(nèi)容如下,等同于之前的sheet.xml
/**
* @Author lsc
* <p> sheet配置類等同于sheet.xml</p>
*/
@Configuration
public 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());
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java8 Lambda表達(dá)式模板方法實(shí)現(xiàn)解析
這篇文章主要介紹了Java8 Lambda表達(dá)式模板方法實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
springboot mybatis里localdatetime序列化問(wèn)題的解決
這篇文章主要介紹了springboot mybatis里localdatetime序列化問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Springboot日志配置的實(shí)現(xiàn)示例
本文主要介紹了Springboot日志配置的實(shí)現(xiàn)示例,使用slf4j和logback的方式記錄日志,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
詳解springboot接口如何優(yōu)雅的接收時(shí)間類型參數(shù)
這篇文章主要為大家詳細(xì)介紹了springboot的接口如何優(yōu)雅的接收時(shí)間類型參數(shù),文中為大家整理了三種常見(jiàn)的方法,希望對(duì)大家有一定的幫助2023-09-09
Java中List根據(jù)map的某個(gè)key去重的代碼
今天小編就為大家分享一篇關(guān)于Java中List根據(jù)map的某個(gè)key去重的代碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Spring boot redis cache的key的使用方法
這篇文章主要介紹了Spring boot redis cache的key的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
mybatis自動(dòng)填充時(shí)間字段示例代碼
這篇文章主要給大家介紹了關(guān)于mybatis自動(dòng)填充時(shí)間字段的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
高并發(fā)系統(tǒng)的限流詳解及實(shí)現(xiàn)
這篇文章主要介紹了高并發(fā)系統(tǒng)的限流詳解及實(shí)現(xiàn),內(nèi)容詳細(xì),小編覺(jué)得很不錯(cuò),這里分享給大家,供需要的朋友參考。隨小編一起看看吧。2017-11-11

