亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

分析Spring框架之設(shè)計(jì)與實(shí)現(xiàn)資源加載器

瀏覽:2日期:2023-07-09 09:37:08
目錄一、前言二、目標(biāo)三、設(shè)計(jì)四、實(shí)現(xiàn)4.1、工程結(jié)構(gòu)4.2、資源加載接口定義和實(shí)現(xiàn)4.3、包裝資源加載器4.4、Bean定義讀取接口4.5、Bean定義抽象類實(shí)現(xiàn)4.6、解析XML處理Bean注冊(cè)五、測試5.1、事先準(zhǔn)備5.2、配置文件5.3、單元測試(資源加載)5.4、單元測試(配置文件注冊(cè)Bean)六、總結(jié)一、前言

你寫的代碼,能接的住產(chǎn)品加需求嗎?

接,是能接的,接幾次也行,哪怕就一個(gè)類一片的 if...else 也可以!但接完成什么樣可就不一定了,會(huì)不會(huì)出事故也不是能控制住的。

那出事故時(shí),你說因?yàn)槲覍?if...else 多了導(dǎo)致代碼爛了,但可是你先動(dòng)的手啊:你說的需求還得加、你說的老板讓上線、你說的合同都簽了,搬磚碼農(nóng)的我沒辦法,才以堆代碼平需求,需求太多不好搞,我才以搬磚平需求!諸侯不服,我才以兵服諸侯,你不服,我就打到你服!

但代碼爛了有時(shí)候并不是因?yàn)樾枨蠹拥目臁⒁膊皇侵鄙暇€。因?yàn)橥诔薪赢a(chǎn)品需求的前幾次,一個(gè)功能邏輯的設(shè)計(jì)并不會(huì)太復(fù)雜,也不會(huì)有多急迫,甚至?xí)舫鲎屇阕鲈O(shè)計(jì)、做評(píng)審、做開發(fā)的時(shí)間,如果這個(gè)時(shí)候仍不能把以后可能會(huì)發(fā)生的事情評(píng)估到需求里,那么導(dǎo)致代碼的混亂從一開始就已經(jīng)埋下了,以后只能越來越亂!

承接需求并能把它做好,這來自于對(duì)需求的理解,產(chǎn)品場景開發(fā)的經(jīng)驗(yàn)以及對(duì)代碼實(shí)踐落地的把控能力等綜合多方面因素的結(jié)果。就像你現(xiàn)在做的開發(fā)中,你的代碼有哪些是經(jīng)常變化的,有哪些是固定通用的,有哪些是負(fù)責(zé)邏輯拼裝的、有哪些是來做核心實(shí)現(xiàn)的。那么現(xiàn)在如果你的核心共用層做了頻繁變化的業(yè)務(wù)層包裝,那么肯定的說,你的代碼即將越來越亂,甚至可能埋下事故的風(fēng)險(xiǎn)!

在我們實(shí)現(xiàn)的 Spring 框架中,每一個(gè)章節(jié)都會(huì)結(jié)合上一章節(jié)繼續(xù)擴(kuò)展功能,就像每一次產(chǎn)品都在加需求一樣,那么在學(xué)習(xí)的過程中可以承上啟下的對(duì)照和參考,看看每一個(gè)模塊的添加都是用什么邏輯和技術(shù)細(xì)節(jié)實(shí)現(xiàn)的。這些內(nèi)容的學(xué)習(xí),會(huì)非常有利于你以后在設(shè)計(jì)和實(shí)現(xiàn),自己承接產(chǎn)品需求時(shí)做的具體開發(fā),代碼的質(zhì)量也會(huì)越來越高,越來越有擴(kuò)展性和可維護(hù)性。

二、目標(biāo)

在完成 Spring 的框架雛形后,現(xiàn)在我們可以通過單元測試進(jìn)行手動(dòng)操作 Bean 對(duì)象的定義、注冊(cè)和屬性填充,以及最終獲取對(duì)象調(diào)用方法。但這里會(huì)有一個(gè)問題,就是如果實(shí)際使用這個(gè) Spring 框架,是不太可能讓用戶通過手動(dòng)方式創(chuàng)建的,而是最好能通過配置文件的方式簡化創(chuàng)建過程。需要完成如下操作:

分析Spring框架之設(shè)計(jì)與實(shí)現(xiàn)資源加載器

如圖中我們需要把步驟:2、3、4整合到Spring框架中,通過 Spring 配置文件的方式將 Bean 對(duì)象實(shí)例化。接下來我們就需要在現(xiàn)有的 Spring 框架中,添加能解決 Spring 配置的讀取、解析、注冊(cè)Bean的操作。

三、設(shè)計(jì)

依照本章節(jié)的需求背景,我們需要在現(xiàn)有的 Spring 框架雛形中添加一個(gè)資源解析器,也就是能讀取classpath、本地文件和云文件的配置內(nèi)容。這些配置內(nèi)容就是像使用 Spring 時(shí)配置的 Spring.xml 一樣,里面會(huì)包括 Bean 對(duì)象的描述和屬性信息。 在讀取配置文件信息后,接下來就是對(duì)配置文件中的 Bean 描述信息解析后進(jìn)行注冊(cè)操作,把 Bean 對(duì)象注冊(cè)到 Spring 容器中。整體設(shè)計(jì)結(jié)構(gòu)如下圖:

分析Spring框架之設(shè)計(jì)與實(shí)現(xiàn)資源加載器

資源加載器屬于相對(duì)獨(dú)立的部分,它位于 Spring 框架核心包下的IO實(shí)現(xiàn)內(nèi)容,主要用于處理Class、本地和云環(huán)境中的文件信息。 當(dāng)資源可以加載后,接下來就是解析和注冊(cè) Bean 到 Spring 中的操作,這部分實(shí)現(xiàn)需要和 DefaultListableBeanFactory 核心類結(jié)合起來,因?yàn)槟闼械慕馕龊蟮淖?cè)動(dòng)作,都會(huì)把 Bean 定義信息放入到這個(gè)類中。 那么在實(shí)現(xiàn)的時(shí)候就設(shè)計(jì)好接口的實(shí)現(xiàn)層級(jí)關(guān)系,包括我們需要定義出 Bean 定義的讀取接口 BeanDefinitionReader 以及做好對(duì)應(yīng)的實(shí)現(xiàn)類,在實(shí)現(xiàn)類中完成對(duì) Bean 對(duì)象的解析和注冊(cè)。四、實(shí)現(xiàn)4.1、工程結(jié)構(gòu)

small-spring-step-05

└── src

    ├── main

    │   └── java

    │       └── cn.bugstack.springframework  

    │           ├── beans

    │           │   ├── factory

    │           │   │   ├── factory

    │           │   │   │   ├── AutowireCapableBeanFactory.java

    │           │   │   │   ├── BeanDefinition.java

    │           │   │   │   ├── BeanReference.java

    │           │   │   │   ├── ConfigurableBeanFactory.java

    │           │   │   │   └── SingletonBeanRegistry.java

    │           │   │   ├── support

    │           │   │   │   ├── AbstractAutowireCapableBeanFactory.java

    │           │   │   │   ├── AbstractBeanDefinitionReader.java

    │           │   │   │   ├── AbstractBeanFactory.java

    │           │   │   │   ├── BeanDefinitionReader.java

    │           │   │   │   ├── BeanDefinitionRegistry.java

    │           │   │   │   ├── CglibSubclassingInstantiationStrategy.java

    │           │   │   │   ├── DefaultListableBeanFactory.java

    │           │   │   │   ├── DefaultSingletonBeanRegistry.java

    │           │   │   │   ├── InstantiationStrategy.java

    │           │   │   │   └── SimpleInstantiationStrategy.java  

    │           │   │   ├── support

    │           │   │   │   └── XmlBeanDefinitionReader.java

    │           │   │   ├── BeanFactory.java

    │           │   │   ├── ConfigurableListableBeanFactory.java

    │           │   │   ├── HierarchicalBeanFactory.java

    │           │   │   └── ListableBeanFactory.java

    │           │   ├── BeansException.java

    │           │   ├── PropertyValue.java

    │           │   └── PropertyValues.java 

    │           ├── core.io

    │           │   ├── ClassPathResource.java 

    │           │   ├── DefaultResourceLoader.java 

    │           │   ├── FileSystemResource.java 

    │           │   ├── Resource.java 

    │           │   ├── ResourceLoader.java 

    │           │   └── UrlResource.java

    │           └── utils

    │               └── ClassUtils.java

    └── test

        └── java

            └── cn.bugstack.springframework.test

                ├── bean

                │   ├── UserDao.java

                │   └── UserService.java

                └── ApiTest.java

Spring Bean 容器資源加載和使用類關(guān)系,如圖 6-3

分析Spring框架之設(shè)計(jì)與實(shí)現(xiàn)資源加載器

本章節(jié)為了能把 Bean 的定義、注冊(cè)和初始化交給 Spring.xml 配置化處理,那么就需要實(shí)現(xiàn)兩大塊內(nèi)容,分別是:資源加載器、xml資源處理類,實(shí)現(xiàn)過程主要以對(duì)接口 Resource、ResourceLoader 的實(shí)現(xiàn),而另外 BeanDefinitionReader 接口則是對(duì)資源的具體使用,將配置信息注冊(cè)到 Spring 容器中去。 在 Resource 的資源加載器的實(shí)現(xiàn)中包括了,ClassPath、系統(tǒng)文件、云配置文件,這三部分與 Spring 源碼中的設(shè)計(jì)和實(shí)現(xiàn)保持一致,最終在 DefaultResourceLoader 中做具體的調(diào)用。 接口:BeanDefinitionReader、抽象類:AbstractBeanDefinitionReader、實(shí)現(xiàn)類:XmlBeanDefinitionReader,這三部分內(nèi)容主要是合理清晰的處理了資源讀取后的注冊(cè) Bean 容器操作。接口管定義,抽象類處理非接口功能外的注冊(cè)Bean組件填充,最終實(shí)現(xiàn)類即可只關(guān)心具體的業(yè)務(wù)實(shí)現(xiàn)

另外本章節(jié)還參考 Spring 源碼,做了相應(yīng)接口的集成和實(shí)現(xiàn)的關(guān)系,雖然這些接口目前還并沒有太大的作用,但隨著框架的逐步完善,它們也會(huì)發(fā)揮作用。如圖 6-4

分析Spring框架之設(shè)計(jì)與實(shí)現(xiàn)資源加載器

BeanFactory,已經(jīng)存在的 Bean 工廠接口用于獲取 Bean 對(duì)象,這次新增加了按照類型獲取 Bean 的方法:<T> T getBean(String name, Class<T> requiredType) ListableBeanFactory,是一個(gè)擴(kuò)展 Bean 工廠接口的接口,新增加了 getBeansOfType、getBeanDefinitionNames() 方法,在 Spring 源碼中還有其他擴(kuò)展方法。 HierarchicalBeanFactory,在 Spring 源碼中它提供了可以獲取父類 BeanFactory 方法,屬于是一種擴(kuò)展工廠的層次子接口。Sub-interface implemented by bean factories that can be part of a hierarchy. AutowireCapableBeanFactory,是一個(gè)自動(dòng)化處理Bean工廠配置的接口,目前案例工程中還沒有做相應(yīng)的實(shí)現(xiàn),后續(xù)逐步完善。 ConfigurableBeanFactory,可獲取 BeanPostProcessor、BeanClassLoader等的一個(gè)配置化接口。 ConfigurableListableBeanFactory,提供分析和修改Bean以及預(yù)先實(shí)例化的操作接口,不過目前只有一個(gè) getBeanDefinition 方法。4.2、資源加載接口定義和實(shí)現(xiàn)

cn.bugstack.springframework.core.io.Resource

public interface Resource { InputStream getInputStream() throws IOException;}

在 Spring 框架下創(chuàng)建 core.io 核心包,在這個(gè)包中主要用于處理資源加載流。定義 Resource 接口,提供獲取 InputStream 流的方法,接下來再分別實(shí)現(xiàn)三種不同的流文件操作:classPath、FileSystem、URL

ClassPath:cn.bugstack.springframework.core.io.ClassPathResource

public class ClassPathResource implements Resource { private final String path; private ClassLoader classLoader; public ClassPathResource(String path) {this(path, (ClassLoader) null); } public ClassPathResource(String path, ClassLoader classLoader) {Assert.notNull(path, 'Path must not be null');this.path = path;this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); } @Override public InputStream getInputStream() throws IOException {InputStream is = classLoader.getResourceAsStream(path);if (is == null) { throw new FileNotFoundException( this.path + ' cannot be opened because it does not exist');}return is; }}

這一部分的實(shí)現(xiàn)是用于通過 ClassLoader 讀取 ClassPath 下的文件信息,具體的讀取過程主要是:classLoader.getResourceAsStream(path)

FileSystem:cn.bugstack.springframework.core.io.FileSystemResource

public class FileSystemResource implements Resource { private final File file; private final String path; public FileSystemResource(File file) {this.file = file;this.path = file.getPath(); } public FileSystemResource(String path) {this.file = new File(path);this.path = path; } @Override public InputStream getInputStream() throws IOException {return new FileInputStream(this.file); } public final String getPath() {return this.path; }}

通過指定文件路徑的方式讀取文件信息,這部分大家肯定還是非常熟悉的,經(jīng)常會(huì)讀取一些txt、excel文件輸出到控制臺(tái)。

Url:cn.bugstack.springframework.core.io.UrlResource

public class UrlResource implements Resource{ private final URL url; public UrlResource(URL url) {Assert.notNull(url,'URL must not be null');this.url = url; } @Override public InputStream getInputStream() throws IOException {URLConnection con = this.url.openConnection();try { return con.getInputStream();}catch (IOException ex){ if (con instanceof HttpURLConnection){((HttpURLConnection) con).disconnect(); } throw ex;} }}

通過 HTTP 的方式讀取云服務(wù)的文件,我們也可以把配置文件放到 GitHub 或者 Gitee 上。

4.3、包裝資源加載器

按照資源加載的不同方式,資源加載器可以把這些方式集中到統(tǒng)一的類服務(wù)下進(jìn)行處理,外部用戶只需要傳遞資源地址即可,簡化使用。

定義接口:cn.bugstack.springframework.core.io.ResourceLoader

public interface ResourceLoader { /** * Pseudo URL prefix for loading from the class path: 'classpath:' */ String CLASSPATH_URL_PREFIX = 'classpath:'; Resource getResource(String location);}

定義獲取資源接口,里面?zhèn)鬟f location 地址即可。

實(shí)現(xiàn)接口:cn.bugstack.springframework.core.io.DefaultResourceLoader

public class DefaultResourceLoader implements ResourceLoader { @Override public Resource getResource(String location) {Assert.notNull(location, 'Location must not be null');if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));}else { try {URL url = new URL(location);return new UrlResource(url); } catch (MalformedURLException e) {return new FileSystemResource(location); }} }}

在獲取資源的實(shí)現(xiàn)中,主要是把三種不同類型的資源處理方式進(jìn)行了包裝,分為:判斷是否為ClassPath、URL以及文件。

雖然 DefaultResourceLoader 類實(shí)現(xiàn)的過程簡單,但這也是設(shè)計(jì)模式約定的具體結(jié)果,像是這里不會(huì)讓外部調(diào)用放知道過多的細(xì)節(jié),而是僅關(guān)心具體調(diào)用結(jié)果即可。

4.4、Bean定義讀取接口

cn.bugstack.springframework.beans.factory.support.BeanDefinitionReader

public interface BeanDefinitionReader { BeanDefinitionRegistry getRegistry(); ResourceLoader getResourceLoader(); void loadBeanDefinitions(Resource resource) throws BeansException; void loadBeanDefinitions(Resource... resources) throws BeansException; void loadBeanDefinitions(String location) throws BeansException;}

這是一個(gè) Simple interface for bean definition readers. 其實(shí)里面無非定義了幾個(gè)方法,包括:getRegistry()、getResourceLoader(),以及三個(gè)加載Bean定義的方法。

這里需要注意 getRegistry()、getResourceLoader(),都是用于提供給后面三個(gè)方法的工具,加載和注冊(cè),這兩個(gè)方法的實(shí)現(xiàn)會(huì)包裝到抽象類中,以免污染具體的接口實(shí)現(xiàn)方法。

4.5、Bean定義抽象類實(shí)現(xiàn)

cn.bugstack.springframework.beans.factory.support.AbstractBeanDefinitionReader

public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader { private final BeanDefinitionRegistry registry; private ResourceLoader resourceLoader; protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {this(registry, new DefaultResourceLoader()); } public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {this.registry = registry;this.resourceLoader = resourceLoader; } @Override public BeanDefinitionRegistry getRegistry() {return registry; } @Override public ResourceLoader getResourceLoader() {return resourceLoader; }}

抽象類把 BeanDefinitionReader 接口的前兩個(gè)方法全部實(shí)現(xiàn)完了,并提供了構(gòu)造函數(shù),讓外部的調(diào)用使用方,把Bean定義注入類,傳遞進(jìn)來。

這樣在接口 BeanDefinitionReader 的具體實(shí)現(xiàn)類中,就可以把解析后的 XML 文件中的 Bean 信息,注冊(cè)到 Spring 容器去了。以前我們是通過單元測試使用,調(diào)用 BeanDefinitionRegistry 完成Bean的注冊(cè),現(xiàn)在可以放到 XMl 中操作了

4.6、解析XML處理Bean注冊(cè)

cn.bugstack.springframework.beans.factory.xml.XmlBeanDefinitionReader

public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {super(registry); } public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {super(registry, resourceLoader); } @Override public void loadBeanDefinitions(Resource resource) throws BeansException {try { try (InputStream inputStream = resource.getInputStream()) {doLoadBeanDefinitions(inputStream); }} catch (IOException | ClassNotFoundException e) { throw new BeansException('IOException parsing XML document from ' + resource, e);} } @Override public void loadBeanDefinitions(Resource... resources) throws BeansException {for (Resource resource : resources) { loadBeanDefinitions(resource);} } @Override public void loadBeanDefinitions(String location) throws BeansException {ResourceLoader resourceLoader = getResourceLoader();Resource resource = resourceLoader.getResource(location);loadBeanDefinitions(resource); } protected void doLoadBeanDefinitions(InputStream inputStream) throws ClassNotFoundException {Document doc = XmlUtil.readXML(inputStream);Element root = doc.getDocumentElement();NodeList childNodes = root.getChildNodes();for (int i = 0; i < childNodes.getLength(); i++) { // 判斷元素 if (!(childNodes.item(i) instanceof Element)) continue; // 判斷對(duì)象 if (!'bean'.equals(childNodes.item(i).getNodeName())) continue;// 解析標(biāo)簽 Element bean = (Element) childNodes.item(i); String id = bean.getAttribute('id'); String name = bean.getAttribute('name'); String className = bean.getAttribute('class'); // 獲取 Class,方便獲取類中的名稱 Class<?> clazz = Class.forName(className); // 優(yōu)先級(jí) id > name String beanName = StrUtil.isNotEmpty(id) ? id : name; if (StrUtil.isEmpty(beanName)) {beanName = StrUtil.lowerFirst(clazz.getSimpleName()); } // 定義Bean BeanDefinition beanDefinition = new BeanDefinition(clazz); // 讀取屬性并填充 for (int j = 0; j < bean.getChildNodes().getLength(); j++) {if (!(bean.getChildNodes().item(j) instanceof Element)) continue;if (!'property'.equals(bean.getChildNodes().item(j).getNodeName())) continue;// 解析標(biāo)簽:propertyElement property = (Element) bean.getChildNodes().item(j);String attrName = property.getAttribute('name');String attrValue = property.getAttribute('value');String attrRef = property.getAttribute('ref');// 獲取屬性值:引入對(duì)象、值對(duì)象Object value = StrUtil.isNotEmpty(attrRef) ? new BeanReference(attrRef) : attrValue;// 創(chuàng)建屬性信息PropertyValue propertyValue = new PropertyValue(attrName, value);beanDefinition.getPropertyValues().addPropertyValue(propertyValue); } if (getRegistry().containsBeanDefinition(beanName)) {throw new BeansException('Duplicate beanName[' + beanName + '] is not allowed'); } // 注冊(cè) BeanDefinition getRegistry().registerBeanDefinition(beanName, beanDefinition);} }}

XmlBeanDefinitionReader 類最核心的內(nèi)容就是對(duì) XML 文件的解析,把我們本來在代碼中的操作放到了通過解析 XML 自動(dòng)注冊(cè)的方式。

loadBeanDefinitions 方法,處理資源加載,這里新增加了一個(gè)內(nèi)部方法:doLoadBeanDefinitions,它主要負(fù)責(zé)解析 xml

在 doLoadBeanDefinitions 方法中,主要是對(duì)xml的讀取 XmlUtil.readXML(inputStream) 和元素 Element 解析。在解析的過程中通過循環(huán)操作,以此獲取 Bean 配置以及配置中的 id、name、class、value、ref 信息。

最終把讀取出來的配置信息,創(chuàng)建成 BeanDefinition 以及 PropertyValue,最終把完整的 Bean 定義內(nèi)容注冊(cè)到 Bean 容器:getRegistry().registerBeanDefinition(beanName, beanDefinition)

五、測試5.1、事先準(zhǔn)備

cn.bugstack.springframework.test.bean.UserDao

public class UserDao { private static Map<String, String> hashMap = new HashMap<>(); static {hashMap.put('10001', '小傅哥');hashMap.put('10002', '八杯水');hashMap.put('10003', '阿毛'); } public String queryUserName(String uId) {return hashMap.get(uId); }}

cn.bugstack.springframework.test.bean.UserService

public class UserService { private String uId; private UserDao userDao; public void queryUserInfo() {return userDao.queryUserName(uId); } // ...get/set}

Dao、Service,是我們平常開發(fā)經(jīng)常使用的場景。在 UserService 中注入 UserDao,這樣就能體現(xiàn)出Bean屬性的依賴了。

5.2、配置文件

important.properties

# Config File

system.key=OLpj9823dZ

spring.xml

<?xml version='1.0' encoding='UTF-8'?><beans> <bean /> <bean class='cn.bugstack.springframework.test.bean.UserService'><property name='uId' value='10001'/><property name='userDao' ref='userDao'/> </bean></beans>

這里有兩份配置文件,一份用于測試資源加載器,另外 spring.xml 用于測試整體的 Bean 注冊(cè)功能。

5.3、單元測試(資源加載)

案例

private DefaultResourceLoader resourceLoader; @Beforepublic void init() { resourceLoader = new DefaultResourceLoader();} @Testpublic void test_classpath() throws IOException { Resource resource = resourceLoader.getResource('classpath:important.properties'); InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content);} @Testpublic void test_file() throws IOException { Resource resource = resourceLoader.getResource('src/test/resources/important.properties'); InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content);} @Testpublic void test_url() throws IOException { Resource resource = resourceLoader.getResource('https://github.com/fuzhengwei/small-spring/important.properties' InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content);}

測試結(jié)果

# Config File

system.key=OLpj9823dZ

Process finished with exit code 0

這三個(gè)方法:test_classpath、test_file、test_url,分別用于測試加載 ClassPath、FileSystem、Url 文件,URL文件在Github,可能加載時(shí)會(huì)慢

5.4、單元測試(配置文件注冊(cè)Bean)

案例

@Testpublic void test_xml() { // 1.初始化 BeanFactory DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // 2. 讀取配置文件&注冊(cè)Bean XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions('classpath:spring.xml'); // 3. 獲取Bean對(duì)象調(diào)用方法 UserService userService = beanFactory.getBean('userService', UserService.class); String result = userService.queryUserInfo(); System.out.println('測試結(jié)果:' + result);}

測試結(jié)果

測試結(jié)果:小傅哥

Process finished with exit code 0

在上面的測試案例中可以看到,我們把以前通過手動(dòng)注冊(cè) Bean 以及配置屬性信息的內(nèi)容,交給了 new XmlBeanDefinitionReader(beanFactory) 類讀取 Spring.xml 的方式來處理,并通過了測試驗(yàn)證。

六、總結(jié) 此時(shí)的工程結(jié)構(gòu)已經(jīng)越來越有 Spring 框架的味道了,以配置文件為入口解析和注冊(cè) Bean 信息,最終再通過 Bean 工廠獲取 Bean 以及做相應(yīng)的調(diào)用操作。 關(guān)于案例中每一個(gè)步驟的實(shí)現(xiàn)小傅哥這里都會(huì)盡可能參照 Spring 源碼的接口定義、抽象類實(shí)現(xiàn)、名稱規(guī)范、代碼結(jié)構(gòu)等,做相應(yīng)的簡化處理。這樣大家在學(xué)習(xí)的過程中也可以通過類名或者接口和整個(gè)結(jié)構(gòu)體學(xué)習(xí) Spring 源碼,這樣學(xué)習(xí)起來就容易多了。 看完絕對(duì)不等于會(huì),你只有動(dòng)起手來從一個(gè)小小的工程框架結(jié)構(gòu),敲到現(xiàn)在以及以后不斷的變大、變多、變強(qiáng)時(shí),才能真的掌握這里面的知識(shí)。另外每一個(gè)章節(jié)的功能實(shí)現(xiàn)都會(huì)涉及到很多的代碼設(shè)計(jì)思路,要認(rèn)真去領(lǐng)悟。當(dāng)然實(shí)踐起來是最好的領(lǐng)悟方式!

以上就是分析Spring框架之設(shè)計(jì)與實(shí)現(xiàn)資源加載器的詳細(xì)內(nèi)容,更多關(guān)于Spring 資源加載器的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲综合激情五月色播 | 91短视频版在线观看免费大全 | 国产一级做a爰片久久毛片男 | 久久福利一区二区三区 | 在线观看日本一区二区 | 成人免费黄色 | 人九九精品 | 国产精品福利无圣光一区二区 | 亚洲人交性视频 | 污污的网址 | 亚洲一区二区三区在线观看蜜桃 | 小明永久免费大陆在线观看 | 日本一级在线 | 国产在线精品福利大全 | 污网址免费 | 伊人久久大香线蕉精品哪里 | 一级日韩片 | 亚洲精品视频在线观看免费 | 国产精品观看 | 和日本免费不卡在线v | 日本二级黄色片 | 国产高清不卡码一区二区三区 | 日日摸夜夜添夜夜添破第一 | 亚洲国产成人久久一区www妖精 | 成人啪啪97丁香 | 在线观看视频黄 | 曰本女人一级毛片看一级毛 | 麻豆精品视频在线原创 | 亚洲人成网站色7799在线观看 | 东京不太热视频高清在线 | 免费jizz在在线播放国产 | 久久亚洲欧美日本精品品 | 刺激花蒂抽搐视频在线看 | 亚洲一二三区视频 | 青青青国产依人免费视频 | 亚洲 欧美 激情 另类 自拍 | 国产成人精品区在线观看 | 免费三片在线观看网站 | 久久一级毛片 | 在线欧美精品国产综合五月 | 亚洲综合图色国模40p |