Spring FactoryBean用法

最近在看spring ioc源码,看到FactoryBean这个内容。这个和BeanFactory的区别

1. BeanFactory: 生成bean的工厂,是一个接口,定义了很多方法

2. FactoryBean: 是一个Bean,生产bean的bean

下面是一个demo,用于获取Person的FactoryBean

Person.java

/**
 * @Author: 
 * @Description:
 * @Date: Created in : 2018/12/10 8:01 AM
 **/
public class Person {
  private String name;
  private int age;
  private String city;

  public String getName() {
    return name;
  }

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

  public int getAge() {
    return age;
  }

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

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  @Override
  public String toString() {
    return "Person{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", city='" + city + '\'' +
        '}';
  }
}

PersonFactoryBean.java

import org.springframework.beans.factory.FactoryBean;

/**
 * @Author: 
 * @Description:
 * @Date: Created in : 2018/12/10 8:02 AM
 **/
public class PersonFactoryBean implements FactoryBean {
  private String personInfo;

  public Person getObject() throws Exception {
    Person person = new Person();
    String[] personInfos = personInfo.split(",");
    person.setName(personInfos[0]);
    person.setAge(Integer.parseInt(personInfos[1]));
    person.setCity(personInfos[2]);
    return person;
  }

  public Class getObjectType() {
    return Person.class;
  }

  public boolean isSingleton() {
    return true;
  }

  public String getPersonInfo() {
    return personInfo;
  }

  public void setPersonInfo(String personInfo) {
    this.personInfo = personInfo;
  }
}

beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="person" class="com.gxf.mybatis.TestFactoryBean.PersonFactoryBean">
        <property name="personInfo">
            <value>guanxianseng,28,chengduvalue>
        property>
    bean>

beans>

Test.java测试一下

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author: 
 * @Description:
 * @Date: Created in : 2018/12/10 8:05 AM
 **/
public class Test {

  public static void main(String[] args) throws Exception {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("mytestbeans.xml");
    PersonFactoryBean personFactoryBean = (PersonFactoryBean) applicationContext.getBean("&person");
    System.out.println("personFactoryBean:" + personFactoryBean);
    System.out.println("personFactoryBean.getObject()" + personFactoryBean.getObject());
    Person person = (Person) applicationContext.getBean("person");
    System.out.println(person);
  }
}

这里主要测了两个内容

1. FactoryBean.getObject()返回Person

2. 从spring容器中获取factoryBean实例的前缀 &

TODO:

1. factorybean 和其他bean实例化有什么不同

2. factorybean 和其他bean从spring中get出来有什么不同

你可能感兴趣的:(Spring FactoryBean用法)