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