1. 程式人生 > >Spring FactoryBean用法

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出來有什麼不同