您好,登錄后才能下訂單哦!
小編給大家分享一下Spring中實例工廠方法和靜態(tài)工廠方法的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
Spring的實例工廠方法和靜態(tài)工廠方法都可以用來實例化bean,本文我們就來看看相關(guān)實例。
靜態(tài)工廠方法:直接調(diào)用靜態(tài)方法可以返回Bean的實例
package com.zhu.string.factory; import java.util.HashMap; import java.util.Map; public class StaticCarFactory { /** * 靜態(tài)工廠方法:直接調(diào)用靜態(tài)方法可以返回Bean的實例 * */ 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); } }
實例工廠方法。即調(diào)用工廠本身,再調(diào)用工廠的實例方法來返回bean實例
package com.zhu.string.factory; import java.util.HashMap; import java.util.Map; public class InstanceCarFactory { /** * 實例工廠方法。即調(diào)用工廠本身,再調(diào)用工廠的實例方法來返回bean實例 */ 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 ><?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)工廠方法實例,而是配置bean實例 --> <!-- 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> <!-- 配置工廠的實例 --> <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實體類
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); } }
運行結(jié)果:
Car [brand=aodi, price=3000.0]
Car [brand=audi, price=1000.0]
看完了這篇文章,相信你對“Spring中實例工廠方法和靜態(tài)工廠方法的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。