Spring FactoryBean用法

Posted Just DO IT by luckygxf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring FactoryBean用法相关的知识,希望对你有一定的参考价值。

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

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

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

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

Person.java

/**
 * @Author: <[email protected]>
 * @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: <[email protected]>
 * @Description:
 * @Date: Created in : 2018/12/10 8:02 AM
 **/
public class PersonFactoryBean implements FactoryBean<Person> {
  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,chengdu</value>
        </property>
    </bean>

</beans>

Test.java测试一下

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

/**
 * @Author: <[email protected]>
 * @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用法的主要内容,如果未能解决你的问题,请参考以下文章

Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法

spring FactoryBean配置Bean

Spring中Bean的配置方式之FactoryBean

Spring源码分析FactoryBean的使用

Spring之FactoryBean

Spring之FactoryBean详解