Spring学习--静态工厂方法、实例工厂方法创建 Bean
通过调用静态工厂方法创建 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
- 在 bean 的factory-bean 属性里指定拥有该工厂方法的bean。
- 在 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="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的更多相关文章
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring第一课:基于XML装配bean(四),三种实例化方式:默认构造、静态工厂、实例工厂
Spring中基于XML中的装配bean有三种方式: 1.默认构造 2.静态工厂 3.实例工厂 1.默认构造 在我们在Spring的xml文件中直接通过: <bean id=" ...
- java:Spring框架2(bean的作用域,静态工厂和实例工厂,自动装配,动态代理)
1.bean的作用域,静态工厂和实例工厂: bean.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
- 【Spring】Spring中的Bean - 2、Baen的实例化 (构造器、静态工厂、实例工厂)
Bean的实例化 文章目录 Bean的实例化 构造器实例化 静态工厂方式实例化 实例工厂方式实例化 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-S ...
- spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试
spring学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试 ioc 概念 控制反转(Inversion of Control,缩写为IOC),是面向 ...
- Spring(十三):使用工厂方法来配置Bean的两种方式(静态工厂方法&实例工厂方法)
通过调用静态工厂方法创建Bean 1)调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不需要关心创建对象的具体细节. 2)要声明通过静态 ...
- Spring实例化Bean三种方法:构造器、静态工厂、实例工厂
Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...
- Spring基础(3) : 静态工厂和实例工厂创建bean
public class Factory { public static Person staticCreate(){ Person p = new Person(); p.name="st ...
- Spring学习之Aop的各种增强方法
AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...
随机推荐
- html生成缩略图来预览解决方案
html生成缩略图来预览解决方案 一.总结 一句话总结:先将html转化为canvas,然后将canvas生成图片ajax上传到服务器,就可以了 html 转化 canvas 图片 上传 html2c ...
- English trip -- Review Unit1 Personal Information 个人信息
1.重点内容进行自我介绍 What's you name? I'm Loki Where are you from? I'm Local, I'm Chengdu How old are you? t ...
- 3-23 Rspec自动化测试(开始练习)
闰年程序 leap_year_spec.rb require_relative './leap_year' describe "Leap Year" do it "201 ...
- Party CodeForces - 906C (状压)
大意: 给定n(n<=22)个人, m个关系谁跟谁是朋友, 朋友关系是双向的, 每次操作可以选择一个人, 使他的朋友互相成为朋友, 求最少多少次操作可以使所有人互相认识 这个题挺巧妙的了, 关键 ...
- div节点的操作(添加,删除,替换,克隆)
<html> <head> <title></title> <style type="text/css"> div{ b ...
- PKU Judge Online 安装指南
一 安装 JDK 1.5 1 下载 到 Sun 官方网站( http://java.sun.com/j2se/1.5.0 /download.jsp )下载 j2sdk ,注意下载为 JDK 5.0 ...
- bind出现Address already in use解决方法
在socket函数和bind函数之间加入一段代码: // 建立服务器端socket if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0))<0) ...
- Shell脚本的学习(一)
Shell脚本的学习(一) 一)代码式shell脚本简介 1.下载 Xshell 5 建一个文件夹 mkdri home/data ; 1)查看一个在data里建一个1.sh 查看是否建立成功. 2) ...
- javaweb web.xml版本
web.xml版本的xsd分为如下几个版本 web-app_2_2.xsd web-app_2_3.xsd web-app_2_4.xsd web-app_2_5.xsd .... web-app_3 ...
- webservice-jdk客户端代码
使用wsimport.exe 生成客户端代码 使用JDK的bin文件夹中,有一个wsimport.exe,这个工具依据wsdl文件生成相应的类文件,然后用这些文件就可以像调用本地类一样调用本地的类一样 ...