Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼
Spring的實(shí)例工廠方法和靜態(tài)工廠方法都可以用來(lái)實(shí)例化bean,本文我們就來(lái)看看相關(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í)例方法來(lái)返回bean實(shí)例
package com.zhu.string.factory;
import java.util.HashMap;
import java.util.Map;
public class InstanceCarFactory {
/**
* 實(shí)例工廠方法。即調(diào)用工廠本身,再調(diào)用工廠的實(shí)例方法來(lái)返回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">
<!-- 通過(guò)靜態(tài)方法來(lái)配置bean,注意不是配置靜態(tài)工廠方法實(shí)例,而是配置bean實(shí)例
-->
<!--
class屬性:指向靜態(tài)方法的全類名 factory-method:指向靜態(tài)方法的名字
constructor-arg:如果工廠方法需要傳入?yún)?shù),則使用constructor-arg來(lái)配
置參數(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)文章
Java 中DateUtils日期工具類的實(shí)例詳解
這篇文章主要介紹了Java 中DateUtils日期工具類的實(shí)例詳解的相關(guān)資料,有時(shí)候開(kāi)發(fā)java項(xiàng)目使用日期類型,這里介紹下日期工具類,需要的朋友可以參考下2017-08-08
Java 覆蓋equals時(shí)總要覆蓋hashcode
這篇文章主要介紹了Java 覆蓋equals時(shí)總要覆蓋hashcode的相關(guān)資料,這里附有實(shí)例代碼,具有參考價(jià)值,需要的朋友可以參考下2016-12-12
Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁(yè)
本文主要介紹了Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁(yè)的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
SpringCloud?Feign實(shí)現(xiàn)微服務(wù)之間相互請(qǐng)求問(wèn)題
Feign是Netflix開(kāi)發(fā)的聲明式、模板化的HTTP客戶端,?Feign可以幫助我們更快捷、優(yōu)雅地實(shí)現(xiàn)微服務(wù)之間的調(diào)用,這篇文章主要介紹了SpringCloud?Feign實(shí)現(xiàn)微服務(wù)之間相互請(qǐng)求,需要的朋友可以參考下2022-06-06
JDK1.8中的ConcurrentHashMap使用及場(chǎng)景分析
這篇文章主要介紹了JDK1.8中的ConcurrentHashMap使用及場(chǎng)景分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
java保留小數(shù)的四種實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了java保留小數(shù)的四種實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

