博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中的Bean配置 (15)——在Spring的IOC容器里配置Bean(通过工厂方法创建Bean)——基于XML文件的方式
阅读量:3962 次
发布时间:2019-05-24

本文共 3437 字,大约阅读时间需要 11 分钟。

在这里插入图片描述

工厂方法有两种:1;静态工厂方法 2;实例工厂方法

如下一一介绍

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

  • 调用静态工厂方法创建 Bean是将对象创建的过程封装到静态方法中. 当客户端需要对象时, 只需要简单地调用静态方法, 而不同关心创建对象的细节.
  • 要声明通过静态方法创建的 Bean, 需要在 Bean 的 class 属性里指定拥有该工厂的方法的类, 同时在 factory-method 属性里指定工厂方法的名称. 最后, 使用 元素为该方法传递方法参数.

代码示例:

Car_StaticFactory.java

package com.atguigu.spring.bean;import java.util.HashMap;import java.util.Map;/** *  .静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例 */public class Car_StaticFactory {
private static Map
cars =new HashMap
(); static {
cars.put("benchi", new Car("benchi",80000)); cars.put("baoma", new Car("baoma",80000)); } //静态工厂方法 public static Car getCar(String name) {
return cars.get(name); }}

Car.java

package com.atguigu.spring.bean;public class Car {
private String brand; private double price; public void setBrand(String brand) {
this.brand = brand; } public void setPrice(Double price) {
this.price = price; } public Car(String brand, double price) {
super(); this.brand = brand; this.price = price; System.out.println("调用构造函数"); } @Override public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]"; } }

application.xml

Main.java

package com.atguigu.spring.bean;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext=new ClassPathXmlApplicationContext("application.xml"); Car car1=(Car) configurableApplicationContext.getBean("car1"); System.out.println(car1); configurableApplicationContext.close(); }}

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

  • 实例工厂方法: 将对象的创建过程封装到另外一个对象实例的方法里. 当客户端需要请求对象时, 只需要简单的调用该实例方法而不需要关心对象的创建细节.
  • 要声明通过实例工厂方法创建的 Bean

—在 bean 的 factory-bean 属性里指定拥有该工厂方法的 Bean

—在 factory-method 属性里指定该工厂方法的名称

—使用 construtor-arg 元素为工厂方法传递方法参数

代码演示

Car_Instance

package com.atguigu.spring.bean;import java.util.HashMap;import java.util.Map;/** * .实例工厂方法,即需要创建工厂本身,在调用工厂的实例方法返回bean的实例 */public class Car_Instance {
private static Map
cars =new HashMap
(); public Car_Instance() {
cars.put("benchi", new Car("benchi", 8000000)); cars.put("baoma", new Car("baoam", 4000000)); } public Car getCar(String brand) {
return cars.get(brand); } }

Car.java

package com.atguigu.spring.bean;public class Car {
private String brand; private double price; public void setBrand(String brand) {
this.brand = brand; } public void setPrice(Double price) {
this.price = price; } public Car(String brand, double price) {
super(); this.brand = brand; this.price = price; System.out.println("调用构造函数"); } @Override public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]"; } }

application.xml

Main.java

package com.atguigu.spring.bean;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext=new ClassPathXmlApplicationContext("application.xml"); Car car2=(Car) configurableApplicationContext.getBean("car2"); System.out.println(car2); configurableApplicationContext.close(); }}

转载地址:http://mrozi.baihongyu.com/

你可能感兴趣的文章
PL/SQL Developer技巧
查看>>
3-python之PyCharm如何新建项目
查看>>
15-python之while循环嵌套应用场景
查看>>
17-python之for循环
查看>>
18-python之while循环,for循环与else的配合
查看>>
19-python之字符串简单介绍
查看>>
20-python之切片详细介绍
查看>>
P24-c++类继承-01详细的例子演示继承的好处
查看>>
P8-c++对象和类-01默认构造函数详解
查看>>
P1-c++函数详解-01函数的默认参数
查看>>
P3-c++函数详解-03函数模板详细介绍
查看>>
P4-c++函数详解-04函数重载,函数模板和函数模板重载,编译器选择使用哪个函数版本?
查看>>
P5-c++内存模型和名称空间-01头文件相关
查看>>
P6-c++内存模型和名称空间-02存储连续性、作用域和链接性
查看>>
P9-c++对象和类-02构造函数和析构函数总结
查看>>
P10-c++对象和类-03this指针详细介绍,详细的例子演示
查看>>
bat备份数据库
查看>>
linux数据库导出结果集且比对 && grep -v ---无法过滤的问题
查看>>
shell函数与自带变量
查看>>
linux下shell获取不到PID
查看>>