BeanFactory:是用來給具體的IOC容器的實現提供了規範,實現了BeanFactory的接口類代表此類是一個工廠,主要負責初始化各類Bean,並調用他們的生命週期方法。該接口也是IOC容器的頂級接口。spring
FactoryBean:是一種工廠bean,能夠返回bean的實例,能夠經過實現該接口對bean進行額外的操做,例如簡化配置等。其實在spring中有兩種bean,一種是普通的bean,一種是工廠bean便是FactoryBean。springboot
FactoryBean和Bean不一樣,其返回的對象不是指定類的一個實例,而是該FactrorBean的getObject方法所返回的對象。app
在通常狀況下,spring經過反射機制利用class屬性,指定實現類實例化Bean,在某些狀況下,實例化Bean的過程比較複雜,若是按照傳統的方式,則須要在中配置大量的配置信息。ide
1:簡化XML配置,隱藏細節:假設一個類有很是多的屬性,咱們想經過spring對類中的屬性進行值的注入,勢必會在配置文件中書寫大量的屬性配置,形成配置文件臃腫,那麼此時能夠考慮使用FactoryBean來簡化配置測試
新建一個beanui
/**
*
* @author Hxd
* @createTime 2020-03-26 20:55:58
**/
public class User {
/** 姓名 */
private String name;
/** 年齡 */
private int age;
/** 班級名稱 */
private String className;
public User() {
}
public User(String name, int age, String className) {
this.name = name;
this.age = age;
this.className = className;
}
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 getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "user{" + "name='" + name + '\'' + ", age=" + age + ", className='" + className + '\'' + '}';
}
}
新建一個FactoryBean
package com.clover.springbootswagger.controller;
import org.springframework.beans.factory.FactoryBean;
/**
*
* @author Huxudong
* @createTime 2020-03-26 21:06:13
**/
public class StudentFactoryBean implements FactoryBean<User> {
private String userInfo;
@Override
public User getObject() throws Exception {
if(userInfo == null) {
throw new IllegalArgumentException("userInfo is required");
}
String[] splitUserInfo = userInfo.split(",");
if(null == splitUserInfo || splitUserInfo.length != 3) {
throw new IllegalArgumentException("userInfo config error");
}
User user = new User();
user.setName(splitUserInfo[0]);
user.setAge(Integer.valueOf(splitUserInfo[1]));
user.setClassName(splitUserInfo[2]);
return user;
}
@Override
public Class<?> getObjectType() {
return User.class;
}
public String getUserInfo() {
return userInfo;
}
public void setUserInfo(String userInfo) {
this.userInfo = userInfo;
}
}
對應的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">
<!--注意:class是StudentFactoryBean而不是Student-->
<bean id="student" class="com.clover.springbootswagger.controller.StudentFactoryBean" p:userInfo="張三,25,三年二班"/>
</beans>
測試類:
import org.mapstruct.AfterMapping;
import org.mapstruct.BeforeMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
@BeforeMapping
public void before() {
System.out.println("測試開始....");
}
@AfterMapping
public void after() {
System.out.println("測試技術");
}
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
System.out.println(applicationContext.getBean("user"));
System.out.println(applicationContext.getBean("&user"));
}
}
結果:
---測試開始---
Student{name='張三', age=25, className='二班'}
org.springframework.beans.factory_bean.UserFactoryBean@25hu568
---測試結束---
這樣咱們就實現了經過BeanFactory接口達到了簡化配置文件的做用。另外你們也能夠發現getBean(「student」)返回的Student類的實例;而getBean("&student")返回的是StudentFactoryBean實例,即工廠bean其自己。this
參考:https://blog.csdn.net/lyc_liyanchao/article/details/82424122