使用BeanFactory實(shí)現(xiàn)創(chuàng)建對(duì)象
本文實(shí)例為大家分享了BeanFactory實(shí)現(xiàn)創(chuàng)建對(duì)象的具體代碼,供大家參考,具體內(nèi)容如下
說明:
其作用是減少層與層之間的依賴。
實(shí)現(xiàn)步驟:
編寫2個(gè)類(Student,Teacher)再編寫beans.properties文件,接著編寫B(tài)eanFactory類,最后編寫測(cè)試類BeanTest。
參考代碼如下:
/** *beans.properties文件的內(nèi)容(位于與src平級(jí)的config資源包下) */ Student=com.xxx.generic.demo.Student Teacher=com.xxx.generic.demo.Teacher /** *BeanFactory類的參考代碼 */ import java.io.InputStream; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Set; public class BeanFactory { private BeanFactory() { } private static Map<String, String> beans = new HashMap<>(); static { InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("beans.properties"); Properties prop = new Properties(); try { prop.load(is); Enumeration<String> keys = (Enumeration<String>) prop.propertyNames(); while (keys.hasMoreElements()) { String key = keys.nextElement(); String value = prop.getProperty(key); beans.put(key, value); } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } } } public static <T> T getBean(Class<T> clazz) { T t = null; String className = clazz.getSimpleName(); Set<String> keys = beans.keySet(); for (String key : keys) { if (key.equals(className)) { String value = beans.get(key); try { t = (T) Class.forName(value).newInstance(); } catch (Exception e) { e.printStackTrace(); } break; } } return t; } } /** *BeanTest類參考代碼 */ public class BeanTest { public static void main(String[] args) { Student s = BeanFactory.getBean(Student.class); System.out.println(s + ":我是" + s.getClass().getSimpleName() + "的一個(gè)對(duì)象。"); Teacher t = BeanFactory.getBean(Teacher.class); System.out.println(t + ":我是" + t.getClass().getSimpleName() + "的一個(gè)對(duì)象。"); } }
運(yùn)行結(jié)果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 項(xiàng)目中使用Swagger2的示例
本篇文章主要介紹了Spring Boot 項(xiàng)目中使用Swagger2的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Javaweb 500 服務(wù)器內(nèi)部錯(cuò)誤的解決
這篇文章主要介紹了Javaweb 500 服務(wù)器內(nèi)部錯(cuò)誤的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java用split分割含一個(gè)或多個(gè)空格的字符串案例
這篇文章主要介紹了Java用split分割含一個(gè)或多個(gè)空格的字符串案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來過來看看吧2020-09-09Springboot整合Flowable6.x導(dǎo)出bpmn20的步驟詳解
這篇文章主要介紹了Springboot整合Flowable6.x導(dǎo)出bpmn20,Flowable流程引擎可用于部署B(yǎng)PMN 2.0流程定義,可以十分靈活地加入你的應(yīng)用/服務(wù)/構(gòu)架,本文給出兩種從flowable導(dǎo)出流程定義bpmn20.xml的方式,需要的朋友可以參考下2023-04-04javaDSL簡(jiǎn)單實(shí)現(xiàn)示例分享
DSL領(lǐng)域定義語言,用來描述特定領(lǐng)域的特定表達(dá)。比如畫圖從起點(diǎn)到終點(diǎn);路由中的從A到B。這是關(guān)于畫圖的一個(gè)簡(jiǎn)單實(shí)現(xiàn)2014-03-03mybatis中查詢結(jié)果為空時(shí)不同返回類型對(duì)應(yīng)返回值問題
這篇文章主要介紹了mybatis中查詢結(jié)果為空時(shí)不同返回類型對(duì)應(yīng)返回值問題,本文分幾種方法給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-10-10解決springboot中mongodb不啟動(dòng)及Dao不能被掃描到的問題
這篇文章主要介紹了解決springboot中mongodb不啟動(dòng)及Dao不能被掃描到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05