Spring基础之 BeanFactory与FactoryBean

BeanFactory

Spring的bean容器, 全限定名是org.springframework.beans.factory.BeanFactory , 所有Ioc容器的根接口。



FactoryBean

一个通用的bean工厂类,用来生产对象。

Spring基础之 BeanFactory与FactoryBean_第1张图片


举例说明:

Student.java 要创建的bean对象

<span style="font-family:Courier New;font-size:14px;">package com.dusk.bean;

public class Student{
	private String id;
	private String name;
	private String age;
	private String sex;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}

	public void sayHello(){
		System.out.println("hello,everyone!");
	}

}</span><span style="font-family:Courier New;font-size:18px;">
</span>

MyFactoryBean.java 生产Student对象的工厂类

<span style="font-family:Courier New;font-size:14px;">package com.dusk.bean;

import org.springframework.beans.factory.FactoryBean;

public class MyFactoryBean implements FactoryBean<Student>{

	@Override
	public Student getObject() throws Exception {
		Student stu=new Student();
		stu.setId("1");
		stu.setName("zhangsan");
		stu.setAge("18");
		stu.setSex("Femal");
		return stu;
	}

	@Override
	public Class<Student> getObjectType() {
		return Student.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}</span><span style="font-size:18px;">
</span>

bean.xml

<span style="font-family:Courier New;font-size:14px;"><?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="factoryBean" class="com.dusk.bean.MyFactoryBean"/>
</beans></span>
Client.java 场景类

<span style="font-family:Courier New;font-size:14px;">package com.dusk.bean;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Client {
	@Test
	public void test(){
		Resource resource=new ClassPathResource("com/dusk/bean/bean.xml");
		BeanFactory beanFactory=new XmlBeanFactory(resource);
		Student student=(Student) beanFactory.getBean("factoryBean");
		Object o=beanFactory.getBean("&factoryBean");
		System.out.println(o.getClass());
		student.sayHello();
		System.out.println(student);
	}
	
}</span><span style="font-size:18px;">
</span>
运行结果:

Spring基础之 BeanFactory与FactoryBean_第2张图片

结论:

BeanFactory.getBean("factoryBean")获取的是factoryBean工厂生产的对象例如上例中的Student对象。

BeanFactory.getBean("&factoryBean")获取的是factoryBean工厂对象本身返回的是com.dusk.bean.MyFactoryBean实例。

你可能感兴趣的:(Spring基础之 BeanFactory与FactoryBean)