Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼
Spring的實(shí)例工廠方法和靜態(tài)工廠方法都可以用來實(shí)例化bean,本文我們就來看看相關(guān)實(shí)例。
靜態(tài)工廠方法:直接調(diào)用靜態(tài)方法可以返回Bean的實(shí)例
package com.zhu.string.factory; import java.util.HashMap; import java.util.Map; public class StaticCarFactory { /** * 靜態(tài)工廠方法:直接調(diào)用靜態(tài)方法可以返回Bean的實(shí)例 * */ private static Map<String ,Car > cars=new HashMap<String , Car>(); static{ cars.put("audi", new Car(3000, "aodi")); cars.put("fodo", new Car(3000, "aodi")); } //靜態(tài)工廠方法 public static Car getCar(String name){ return cars.get(name); } }
實(shí)例工廠方法。即調(diào)用工廠本身,再調(diào)用工廠的實(shí)例方法來返回bean實(shí)例
package com.zhu.string.factory; import java.util.HashMap; import java.util.Map; public class InstanceCarFactory { /** * 實(shí)例工廠方法。即調(diào)用工廠本身,再調(diào)用工廠的實(shí)例方法來返回bean實(shí)例 */ private Map<String ,Car> cars=null; public InstanceCarFactory() { // TODO Auto-generated constructor stub cars=new HashMap<String, Car>(); cars.put("audi", new Car(1000,"audi")); cars.put("dffdas", new Car(2000,"audi")); } public Car getCar(String brand){ return cars.get(brand); } }
beans-factory.xml
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 通過靜態(tài)方法來配置bean,注意不是配置靜態(tài)工廠方法實(shí)例,而是配置bean實(shí)例 --> <!-- class屬性:指向靜態(tài)方法的全類名 factory-method:指向靜態(tài)方法的名字 constructor-arg:如果工廠方法需要傳入?yún)?shù),則使用constructor-arg來配 置參數(shù) --> <bean id="car1" factory-method="getCar" class="com.zhu.string.factory.StaticCarFactory"> <constructor-arg value="audi"></constructor-arg> </bean> <!-- 配置工廠的實(shí)例 --> <bean id="carFactory" class="com.zhu.string.factory.InstanceCarFactory"> </bean> <bean id="car2" factory-bean="carFactory" factory-method="getCar"> <constructor-arg value="audi"></constructor-arg> </bean> </beans></span>
Car.java實(shí)體類
package com.zhu.string.factory; public class Car { private double price; private String brand; public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + "]"; } public Car(){ System.out.println("cars....constructor"); } public Car(double price, String brand) { super(); this.price = price; this.brand = brand; } }
Main.java
package com.zhu.string.factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext cx=new ClassPathXmlApplicationContext("beans- factory.xml"); Car car1=(Car) cx.getBean("car1"); System.out.println(car1); Car car2=(Car) cx.getBean("car2"); System.out.println(car2); } }
運(yùn)行結(jié)果:
Car [brand=aodi, price=3000.0]
Car [brand=audi, price=1000.0]
總結(jié)
以上就是本文關(guān)于Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- Spring實(shí)戰(zhàn)之調(diào)用實(shí)例工廠方法創(chuàng)建Bean操作示例
- Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作示例
- Spring工廠方法創(chuàng)建(實(shí)例化)bean實(shí)例代碼
- 使用spring工廠讀取property配置文件示例代碼
- 使用spring的IOC解決程序耦合的方法
- java Spring松耦合高效應(yīng)用簡(jiǎn)單實(shí)例分析
- 微信小程序 springboot后臺(tái)如何獲取用戶的openid
- Linux 啟動(dòng)停止SpringBoot jar 程序部署Shell 腳本的方法
- 如何基于Spring使用工廠模式實(shí)現(xiàn)程序解耦
相關(guān)文章
SpringBoot之Helloword 快速搭建一個(gè)web項(xiàng)目(圖文)
這篇文章主要介紹了SpringBoot之Helloword 快速搭建一個(gè)web項(xiàng)目(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Java中byte輸出write到文件的實(shí)現(xiàn)方法講解
今天小編就為大家分享一篇關(guān)于Java中byte輸出write到文件的實(shí)現(xiàn)方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03mybatis多個(gè)區(qū)間處理方式(雙foreach循環(huán))
這篇文章主要介紹了mybatis多個(gè)區(qū)間處理方式(雙foreach循環(huán)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot超詳細(xì)講解集成Flink的部署與打包方法
昨天折騰了下SpringBoot與Flink集成,實(shí)際上集成特簡(jiǎn)單,主要是部署打包的問題折騰了不少時(shí)間。想打出的包直接可以java -jar運(yùn)行,同時(shí)也可以flink run運(yùn)行,或者在flink的dashboard上上傳點(diǎn)擊啟動(dòng)。結(jié)果是不行,但是使用不同的插件打包還是可以的2022-05-05JPA設(shè)置默認(rèn)字段及其長(zhǎng)度詳解
JPA是Java Persistence API的簡(jiǎn)稱,中文名Java持久層API,是JDK 5.0注解或XML描述對(duì)象-關(guān)系表的映射關(guān)系,并將運(yùn)行期的實(shí)體對(duì)象持久化到數(shù)據(jù)庫(kù)中。本文主要介紹了JPA如何設(shè)置默認(rèn)字段及其長(zhǎng)度,感興趣的同學(xué)可以了解一下2021-12-12Java中如何將json字符串轉(zhuǎn)換成map/list
這篇文章主要介紹了Java中如何將json字符串轉(zhuǎn)換成map/list,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07java實(shí)現(xiàn)短地址服務(wù)的方法(附代碼)
大多數(shù)情況下URL太長(zhǎng),字符多,不便于發(fā)布復(fù)制和存儲(chǔ),本文就介紹了通過java實(shí)現(xiàn)短地址服務(wù),減少了許多使用太長(zhǎng)URL帶來的不便,需要的朋友可以參考下2015-07-07如何在spring官網(wǎng)查找XML基礎(chǔ)配置文件
這篇文章主要介紹了如何在spring官網(wǎng)查找XML基礎(chǔ)配置文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10