1、静态工厂方法:

bean

package com.spring.factory;

public class Car {

    public Car(String brand) {
this.brand = brand;
} @Override
public String toString() {
return "Car [brand=" + brand + "]";
} private String brand; public void setBrand(String brand){
System.out.println("setBrand...");
this.brand = brand;
} public void init(){
System.out.println("init...");
} public void destroy(){
System.out.println("destroy...");
} }

2、静态工厂类

package com.spring.factory;

import java.util.HashMap;
import java.util.Map; /*
* 静态工厂:直接调用某一个类的静态方法返回bean的实例
*/
public class staticCarFactory { private static Map<String, Car> cars = new HashMap<String, Car>(); static{
cars.put("audi", new Car("audi"));
cars.put("nission", new Car("nission"));
} //在不创建类的对象的条件下,通过静态方法返回对应的实例
public static Car getCar(String name){
return cars.get(name);
}
}

3、main方法

package com.spring.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-factory.xml");
Car car = (Car)ctx.getBean("factory");
System.out.println(car);
}
}

4、配置bean-factory.xml

<?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-3.0.xsd"> <!-- class 属性 指向静态方法的全类名
factory-method 指向静态方法
constructor-arg 如果工厂方法需要传入参数,则使用constructor-arg 来配置参数
-->
<bean id="factory" class="com.spring.factory.staticCarFactory"
factory-method="getCar">
<constructor-arg value="nission"></constructor-arg>
</bean>
</beans>

工厂方法:

package com.spring.factory;

import java.util.HashMap;
import java.util.Map; public class instanceCarFactory { private Map<String, Car> cars = new HashMap<String, Car>(); public instanceCarFactory() {
cars = new HashMap<String, Car>();
cars.put("audi", new Car("audi"));
cars.put("ford", new Car("ford"));
} public Car getCar(String name){
return cars.get(name);
}
}

xml

    <!-- 配置工厂的实例 -->
<bean id="instanceFactory" class="com.spring.factory.instanceCarFactory"></bean>
<!-- -->
<bean id="car2" factory-bean="instanceFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>
    public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-factory.xml");
Car car = (Car)ctx.getBean("car2");
System.out.println(car);
}

Spring4.0学习笔记(6) —— 通过工厂方法配置Bean的更多相关文章

  1. Spring学习记录(九)---通过工厂方法配置bean

    1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...

  2. Spring4学习回顾之路07- 通过工厂方法配置Bean

    一:通过静态工厂配置Bean 建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Des ...

  3. 4.spriing:Bean的生命周期/工厂方法配置Bean/FactoryBean

    1.Bean的生命周期 scope:singleton/prototype 1)spring容器管理singleton作用的生命周期,spring能够精确知道Bean合适创建,何时初始化完成,以及何时 ...

  4. 11.Spring通过工厂方法配置Bean

    通过工厂方法配置Bean暴扣静态工厂方法和实例工厂方法. 1.静态工厂方法 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不去关心创建 ...

  5. Spring初学之通过工厂方法配置Bean

    工厂方法配置bean分为两种,一种是实例工厂方法,另一种是静态工厂方法. 先来看看实体bean: Car.java: package spring.beans.factory; public clas ...

  6. 工厂方法配置bean

    1:静态工厂方法配置bean 1):对象 package com.spring.helloworld; public class Car { private String name; private ...

  7. C#设计模式学习笔记:(2)工厂方法模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7567880.html,记录一下学习过程以备后续查用. 一.引言 接上一篇C#设计模式学习笔记:简单工厂模式( ...

  8. Spring4.0学习笔记(5) —— 管理bean的生命周期

    Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...

  9. spring 通过工厂方法配置Bean

    概要: 通过调用静态工厂方法创建Bean 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当client须要对象时,仅仅须要简单地调用静态方法,而不用关心创建对象地细节. 要声明通过静 ...

随机推荐

  1. weblogic启动报错之WLS_DIAGNOSTICS000000.DAT

    查看控制台日志报错信息如下: <-- 下午04时46分42秒 CST> <Notice> <Log Management> <BEA-> <The ...

  2. 数据结构——POJ 1686 Lazy Math Instructor 栈的应用

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  3. mysql 一般操作

    mysql -u root -p ->mysql show databases; ->mysql use [database_name]; ->mysql show tables; ...

  4. CodeForces 221(div 2)

    A 无trick水题... /* * Author: Plumrain * Created Time: 2013-12-24 22:26 * File Name: B.cpp */ #include ...

  5. openvpn服务器端配置文件

  6. Fiddler 模拟post 提交

    在使用Fiddler 提交post表单的时候, 一定要加上以下code: Content-Type: application/x-www-form-urlencoded 意思为: 窗体数据被编码为名称 ...

  7. Prefix.pch的作用和用法

    一般用于放置宏,省去xcode编译的时间 Hello World_Prefix.pch:扩展名.pch表示"precompiled header",这是一个你工程要用到的来自于外部 ...

  8. OS开发 touch事件的优先级和事件传递

    界面类的对象一般都是可以接触点击事件的,只不过有的默认接受,有的需要设定属性. userInteractionEnabled 属性设置为YES的时候就可以接受点击事件了 - (void)touches ...

  9. CSS3伪类选择器 图示

         

  10. Swift中FDMB的使用(增、删、改、查)

    直接上代码: import UIKit class ZWDBManager: NSObject { //前提将FMDBDatabase的头文件增加到桥接文件里 var dataBase:FMDatab ...