版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/85123998
通过调用静态工厂方法创建 Bean
- 调用静态工厂方法创建 Bean是将对象创建的过程封装到静态方法中. 当客户端需要对象时, 只需要简单地调用静态方法, 而不同关心创建对象的细节.
- 要声明通过静态方法创建的 Bean, 需要在 Bean 的 class 属性里指定拥有该工厂的方法的类, 同时在 factory-method 属性里指定工厂方法的名称. 最后, 使用 <constrctor-arg> 元素为该方法传递方法参数.
-
demo如下
属性类
package cn.com.day05;
public class Fruit {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Fruit [name=" + name + ", color=" + color + "]";
}
public Fruit() {
// TODO Auto-generated constructor stub
}
public Fruit(String name, String color) {
super();
this.name = name;
this.color = color;
}
}
静态工厂实例化对象的属性,封装获取对象的方法
要十分的注意:这里的一切的一切都是静态的;
而且方法必须是get+对象名,不然会报错,我已经试过了;
package cn.com.day05;
import java.util.HashMap;
import java.util.Map;
public class StaticFactory {
/*静态工厂的创建,在静态代码块里面就已经实例化对象了
* 提供一个方法获取对象
*
*/
public static Map<String, Fruit> fu = new HashMap<String, Fruit>();
// 在静态代码块里面给map初始化
static {
// fu的属性是static
fu.put("apple", new Fruit("apple", "red"));
fu.put("balana", new Fruit("balana", "yellow"));
}
//一定要注意这个方法也必须是static类型的
public static Fruit getfruit(String name) {
return fu.get(name);
}
}
配置文件如下
<?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">
<!-- class是静态工厂的全类名
factory-method="getfruit" 是静态工厂的静态方法的名称
如果这个方法里面需要传递参数的话,就需要有
<constructor-arg value="apple"></constructor-arg>
value="传递的参数的值"
-->
<bean id="staticfactory" class="cn.com.day05.StaticFactory" factory-method="getfruit">
<constructor-arg value="apple"></constructor-arg>
</bean>
</beans>
测试类
package cn.com.day05;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//1.创建IOC容器
ApplicationContext cx=new ClassPathXmlApplicationContext("bean-factory.xml");
//特别注意这里,虽然获取的是staticfactory,但是方法返回的结果类型却是Fruit类型
Fruit sx=(Fruit) cx.getBean("staticfactory");
//打印结果
System.out.println(sx);
}
}
结果如下
通过调用实例工厂方法创建 Bean
实例工厂方法: 将对象的创建过程封装到另外一个对象实例的方法里. 当客户端需要请求对象时, 只需要简单的调用该实例方法而不需要关心对象的创建细节.
要声明通过实例工厂方法创建的 Bean
在 bean 的 factory-bean 属性里指定拥有该工厂方法的 Bean
在 factory-method 属性里指定该工厂方法的名称
使用 construtor-arg 元素为工厂方法传递方法参数
demo如下
工厂类
package cn.com.day05;
import java.util.HashMap;
import java.util.Map;
public class InFactory {
/*
* 实例工厂
*
*/
private Map<String,Fruit> maps=new HashMap<String, Fruit>();
public InFactory(){
maps.put("pear", new Fruit("pear","yellow"));
maps.put("orange", new Fruit("orange","red"));
}
public Fruit getF(String name){
return maps.get(name);
}
}
测试类
package cn.com.day05;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//1.创建IOC容器
ApplicationContext cx=new ClassPathXmlApplicationContext("bean-factory.xml");
//特别注意这里,虽然获取的是staticfactory,但是方法返回的结果类型却是Fruit类型
Fruit sx=(Fruit) cx.getBean("staticfactory");
//打印结果
System.out.println(sx);
Fruit sxs=(Fruit) cx.getBean("fa");
System.out.println(sxs);
}
}
配置文件
<!-- 实例工厂-->
<!-- 两个bean,一个是工厂bean,提供id和class
一个是 操作用的bean,显示工厂bean的名称和方法,如果有参数的话,
<constructor-arg value="apple"></constructor-arg>
value="传递的参数的值"
-->
<bean id="factory" class="cn.com.day05.InFactory"></bean>
<bean id="fa" factory-bean="factory" factory-method="getF">
<constructor-arg value="pear"></constructor-arg>
</bean>
打印结果