bean静态工厂方法,实例工厂方法

关键代码:

<!-- 一、通过静态工厂方法来配置bean -->
 <bean id="user1" 
	class="com.shuoeasy.test.StaticUserFactory"
	factory-method="getUser">
	<constructor-arg value="李四"></constructor-arg>
</bean>

<!-- 二、通过实例工厂方法配置bean(需要分两步实现) -->
<!-- 1.配置工厂实例 -->
<bean id="userFactory" class="com.shuoeasy.test.InstanceUserFactory"></bean>
<!-- 通过工厂实例配置
	factory-bean:指向实例工厂方法的bean
	factory-method:工厂方法的名字
 -->
<bean id="user2"
	factory-bean="userFactory"
	factory-method="getUser">
	<constructor-arg value="赵六"></constructor-arg>
</bean>


详细:

静态工厂方法:

User.java:

package com.shuoeasy.test;

public class User {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public User(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
}

StaticUserFactory.java:

package com.shuoeasy.test;

import java.util.HashMap;
import java.util.Map;

/**
 * 静态工厂
 *
 */
public class StaticUserFactory {
	private static Map<String,User> users= new HashMap<String,User>();
	
	static{
		users.put("张三", new User(1001,"张三"));
		users.put("李四", new User(1001,"李四"));
	}
	
	public static User getUser(String name){
		return users.get(name);
	}
}

bean.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.xsd">

	<!-- 通过静态工厂方法来配置bean -->
	 <bean id="user1" 
		class="com.shuoeasy.test.StaticUserFactory"
		factory-method="getUser">
		<constructor-arg value="李四"></constructor-arg>
	</bean>
</beans>

Main.java:

package com.shuoeasy.test;

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.xml");
		
		// 静态工厂bean
		User user1 = (User) ctx.getBean("user1");
		System.out.println("user1->" + user1);
	}

}

输出:

user1->User [id=1001, name=李四]

实例工厂方法:

InstanceUserFactory.java:

package com.shuoeasy.test;

import java.util.HashMap;
import java.util.Map;

/**
 * 实例工厂
 *
 */
public class InstanceUserFactory {
	private Map<String,User> users = null;
	
	public InstanceUserFactory(){
		users = new HashMap<String,User>();
		users.put("王五", new User(1003,"王五"));
		users.put("赵六", new User(1004,"赵六"));
	}
	
	public User getUser(String name){
		return users.get(name);
	}
}

mean.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.xsd">

	
	<!-- 通过实例工厂方法配置bean(需要分两步实现) -->
	<!-- 1.配置工厂实例 -->
	<bean id="userFactory" class="com.shuoeasy.test.InstanceUserFactory"></bean>
	<!-- 通过工厂实例配置
		factory-bean:指向实例工厂方法的bean
		factory-method:工厂方法的名字
	 -->
	<bean id="user2"
		factory-bean="userFactory"
		factory-method="getUser">
		<constructor-arg value="赵六"></constructor-arg>
	</bean>
</beans>

Main.java:

package com.shuoeasy.test;

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.xml");

		// 实例工厂bean
		User user2 = (User) ctx.getBean("user2");
		System.out.println("user2->" + user2);
	}
}

输出:

user2->User [id=1004, name=赵六]

你可能感兴趣的文章