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的 ...
随机推荐
- mount ntfs-3g , fstab里的配置没有效果
把ntfs-3g配置在 fstab 里,mount 时会报 No such device 网上也有在嵌入式系统里发生的类似例子. 没有解决方法,也不准备再研究了. 准备在机器启动之后,手动下面的命令 ...
- 增加centos7.3上安装php7的php-soap扩展
代码传到正式服务器上去就:Class 'SoapClient' not found,只能是soap扩展没装! 因为服务器上面的PHP是7.1.11的,所以soap也要装7.1.11的,否则会冲突 ...
- [Android教程] Cordova开发App入门(一)创建android项目
前言 Apache Cordova是一个开源的移动开发框架.允许使用标准的web技术-HTML5,CSS3和JavaScript做跨平台开发. 应用在每个平台的具体执行被封装了起来,并依靠符合标准的A ...
- 基因家族收缩和扩张分析 & Selective loss pathway & 泛基因组
套路 这通常就是基因组组装后的必做分析,通过比较基因组学的手段进行分析,可以知道所研究物种在进化过程中哪些核心基因家族发生了变化,从而导致了其特殊的适应性机制的形成. 参考: Extremotoler ...
- wpf里窗体嵌入winform控件被覆盖问题
问题1:嵌套Winform控件(ZedGraph)在WPF的ScrollViewer控件上,出现滚动条,无论如何设置该Winform控件都在顶层,滚动滚动条会覆盖其他WPF控件. 解决办法:在Sc ...
- English trip -- VC(情景课)4 D
What do you see? I can see three men in the pictrue. one older man is a doctor, two younger men are ...
- vue 表单校验(二)
vue 表单校验(二) vue element-ui表单校验 由于现在使用element-ui进行form表单校验,因而使用其自带的校验规则进行校验,发现有些并不是那么好校验,或者说是校验起来很繁琐, ...
- 标准库头文件 (CA2T)
标准库中,CA2T,CA2W的头文件是: #include <atlstr.h>
- 061——VUE中vue-router之通过程序控制路由跳转
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PHP:第三章——PHP中返回引用的函数
<?php header("Content-Type:text/html;charset=utf-8"); $i=1; function &F(){ global $ ...