通过调用静态工厂方法创建 bean:

  • 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节。
  • 要声明通过静态方法创建的 bean , 需要在 bean 的 class 属性里面指定拥有该工厂的方法的类 , 同时在 factory-method 属性里指定工厂方法的名称。最后 , 使用 <constructor-arg> 元素为该方法传递方法参数。
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car" class="com.itdoc.spring.factory.StaticFactory"
factory-method="getCar">
<constructor-arg value="Maserati"/>
</bean> </beans>
 package com.itdoc.spring.factory;

 /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 19:30
*/
public class Car { private String brand; private double price; public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public Car(String brand, double price) {
this.brand = brand;
this.price = price;
} public Car() {
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
 package com.itdoc.spring.factory;

 import java.util.HashMap;
import java.util.Map; /**
* 静态工厂方法
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 19:27
*/
public class StaticFactory { public static Map<String, Car> cars = new HashMap<String, Car>(); static {
cars.put("Ferrari", new Car("Ferrari", 25000000));
cars.put("Maserati", new Car("Maserati", 2870000));
} public static Car getCar(String name) {
return cars.get(name);
} }
 package com.itdoc.spring.factory;

 import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 19:41
*/
public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("factory-beans.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car); }
}

控制台输出:

Car{brand='Maserati', price=2870000.0}

通过调用实例工厂方法创建 bean:

  • 实例工厂方法:将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时 , 只需要简单的调用该实例方法而不需要关心对象的创建细节。
  • 要声明通过实例工厂方法创建的 bean
  1. 在 bean 的factory-bean 属性里指定拥有该工厂方法的bean。
  2. 在 factory-method 属性里指定该工厂方法的名称。
  3. 使用 <constructor-arg> 元素为工厂方法传递方法参数。
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="carFactory" class="com.itdoc.spring.factory.InstanceFactory"></bean> <bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="Ferrari"/>
</bean> </beans>
 package com.itdoc.spring.factory;

 import java.util.HashMap;
import java.util.Map; /**
* 实例工厂方法
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 20:02
*/
public class InstanceFactory { private Map<String, Car> cars = null; public InstanceFactory() {
cars = new HashMap<String, Car>();
cars.put("Ferrari", new Car("Ferrari", 25000000));
cars.put("Maserati", new Car("Maserati", 2870000));
} public Car getCar(String name) {
return cars.get(name);
}
}
 package com.itdoc.spring.factory;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 19:41
*/
public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("factory-beans.xml"); car = (Car) ctx.getBean("car2");
System.out.println(car); }
}

控制台输出:

Car{brand='Ferrari', price=2.5E7}

Spring学习--静态工厂方法、实例工厂方法创建 Bean的更多相关文章

  1. [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Spring第一课:基于XML装配bean(四),三种实例化方式:默认构造、静态工厂、实例工厂

    Spring中基于XML中的装配bean有三种方式: 1.默认构造 2.静态工厂 3.实例工厂 1.默认构造 在我们在Spring的xml文件中直接通过:     <bean id=" ...

  3. java:Spring框架2(bean的作用域,静态工厂和实例工厂,自动装配,动态代理)

    1.bean的作用域,静态工厂和实例工厂: bean.xml: <?xml version="1.0" encoding="UTF-8"?> < ...

  4. 【Spring】Spring中的Bean - 2、Baen的实例化 (构造器、静态工厂、实例工厂)

    Bean的实例化 文章目录 Bean的实例化 构造器实例化 静态工厂方式实例化 实例工厂方式实例化 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-S ...

  5. spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试

    spring学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试 ioc 概念 控制反转(Inversion of Control,缩写为IOC),是面向 ...

  6. Spring(十三):使用工厂方法来配置Bean的两种方式(静态工厂方法&实例工厂方法)

    通过调用静态工厂方法创建Bean 1)调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不需要关心创建对象的具体细节. 2)要声明通过静态 ...

  7. Spring实例化Bean三种方法:构造器、静态工厂、实例工厂

    Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...

  8. Spring基础(3) : 静态工厂和实例工厂创建bean

    public class Factory { public static Person staticCreate(){ Person p = new Person(); p.name="st ...

  9. Spring学习之Aop的各种增强方法

    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...

随机推荐

  1. 滑动窗口解决Substring Search Problem

    2018-07-18 11:19:19 一.Minimum Window Substring 问题描述: 问题求解: public String minWindow(String s, String ...

  2. UEditor自动调节宽度

    UEditor自动调节宽度 一.总结 一句话总结:ueditor是网页的产物,没有API我们照样可以像调网页元素那样调,一样的,这里是改变插件的css样式实现 启示: 百度 文档 引擎 ueditor ...

  3. Edge coloring of bipartite graph CodeForces - 600F (二分图染色)

    大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少 最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$ 若t0 ...

  4. ASP.NET MVC 习惯

  5. Java远程调试 java -Xdebug各参数说明

    JAVA自身支持调试功能,并提供了一个简单的调试工具--JDB,类似于功能强大的GDB,JDB也是一个字符界面的 调试环境,并支持设置断点,支持线程线级的调试 JAVA的调试方法如下: 1.首先支持J ...

  6. httpclient 连接参数

    http.socket.timeout(读取超时) 套接字毫秒级超时时间(SO_TIMEOUT),这就是等待数据,换句话说,在两个连续的数据包之间最大的闲置时间. 如果超时是0表示无限大的超时时间,即 ...

  7. 快速切题 poj2488 A Knight's Journey

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31195   Accepted: 10 ...

  8. System.out.println(i++); System.out.println(++i);的区别

    之前一直对i++和++i很模糊,这次通过两个小demo来探究下. 例1: public static void main(String[] args) { int i=2; System.out.pr ...

  9. 浅谈:当程序员的N多好处,逆袭高富师

    选择一份职业,除了要要分析有没有钱途外(为什么要选择 IT 行业,IT 业有多火爆你造吗?),还要平衡其他方面的利弊.有很多想进入这个行业的小伙伴问我,程序员到底有什么好处.看样子这是很多小伙伴关心的 ...

  10. JavaScript世界的一等公民 - 函数

    简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...