日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

BeanFactory和FactoryBean區(qū)別

作者:Evan Wang 更新時間: 2023-07-22 編程語言

目錄

一、BeanFactory

1、簡單介紹

2、定義方法

3、主要實現(xiàn)類(包括抽象類)

?4、使用方式

二、FactoryBean

1、簡單介紹

?2、定義方法

3、常用類

4、使用方式

三、主要區(qū)別


一、BeanFactory

1、簡單介紹

這個其實是所有Spring Bean的容器根接口,給Spring 的容器定義一套規(guī)范,給IOC容器提供了一套完整的規(guī)范,比如我們常用到的getBean方法等。

進(jìn)入到這個類,我們可以看到如下注釋,意思是:訪問Spring bean容器的根接口。

2、定義方法

  • getBean(String name):?Spring容器中獲取對應(yīng)Bean對象的方法,如存在,則返回該對象。
  • containsBean(String name):判斷Spring容器中是否存在該對象。
  • isSingleton(String name):通過beanName判斷是否為單例對象。
  • isPrototype(String name):判斷bean對象是否為多例對象。
  • isTypeMatch(String name, ResolvableType typeToMatch):判斷name值獲取出來的bean與typeToMath是否匹配。
  • getType(String name):獲取Bean的Class類型。
  • getAliases(String name):獲取name所對應(yīng)的所有的別名。

3、主要實現(xiàn)類(包括抽象類)

  • AbstractBeanFactory:抽象Bean工廠,絕大部分的實現(xiàn)類,都是繼承于它。
  • DefaultListableBeanFactory:Spring默認(rèn)的工廠類。
  • XmlBeanFactory:前期使用XML配置用的比較多的時候用的Bean工廠。
  • AbstractXmlApplicationContext:抽象應(yīng)用容器上下文對象。
  • ClassPathXmlApplicationContext:XML解析上下文對象,用戶創(chuàng)建Bean對象我們早期寫Spring的時候用的就是它。

?4、使用方式

BeanFactory的使用方式有很多,這里就不一一列舉了,具體請查看源碼。

舉一個簡單的例子,使用ClassPathXmlApplicationContext讀取對應(yīng)的xml文件實例對應(yīng)上下文對象:

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
BeanFactory factory = (BeanFactory) context;

二、FactoryBean

1、簡單介紹

該類是SpringIOC容器是創(chuàng)建Bean的一種形式,這種方式創(chuàng)建Bean會有加成方式,融合了簡單的工廠設(shè)計模式于裝飾器模式。
* Interface to be implemented by objects used within a {@link BeanFactory} which * are themselves factories for individual objects. If a bean implements this * interface, it is used as a factory for an object to expose, not directly as a * bean instance that will be exposed itself.
有些人就要問了,我直接使用Spring默認(rèn)方式創(chuàng)建Bean不香么,為啥還要用FactoryBean做啥,在某些情況下,對于實例Bean對象比較復(fù)雜的情況下,使用傳統(tǒng)方式創(chuàng)建bean會比較復(fù)雜,例如(使用xml配置),這樣就出現(xiàn)了FactoryBean接口,可以讓用戶通過實現(xiàn)該接口來自定義該Bean接口的實例化過程。即包裝一層,將復(fù)雜的初始化過程包裝,讓調(diào)用者無需關(guān)系具體實現(xiàn)細(xì)節(jié)。

?2、定義方法

  • T getObject():返回實例
  • Class<?> getObjectType():返回該裝飾對象的Bean的類型
  • default boolean isSingleton():Bean是否為單例

3、常用類

  • ProxyFactoryBean:Aop代理Bean
  • GsonFactoryBean:Gson

4、使用方式

Spring XML方式:

application.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.xsd">

    <bean id="demo" class="cn.lonecloud.spring.example.bo.Person">
        <property name="age" value="10"/>
        <property name="name" value="xiaoMing"/>
    </bean>

    <bean id="demoFromFactory" class="cn.lonecloud.spring.example.bean.PersonFactoryBean">
        <property name="initStr" value="10,init from factory"/>
    </bean>
</beans>

personFactoryBean

package cn.lonecloud.spring.example.bean;

import cn.lonecloud.spring.example.bo.Person;
import org.springframework.beans.factory.FactoryBean;

import java.util.Objects;

public class PersonFactoryBean implements FactoryBean<Person> {

    /**
     * 初始化Str
     */
    private String initStr;

    @Override
    public Person getObject() throws Exception {
        //這里我需要獲取對應(yīng)參數(shù)
        Objects.requireNonNull(initStr);
        String[] split = initStr.split(",");
        Person p=new Person();
        p.setAge(Integer.parseInt(split[0]));
        p.setName(split[1]);
        //這里可能需要還要有其他復(fù)雜事情需要處理
        return p;
    }

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

    public String getInitStr() {
        return initStr;
    }

    public void setInitStr(String initStr) {
        this.initStr = initStr;
    }
}

main方法

package cn.lonecloud.spring.example.factory;

import cn.lonecloud.spring.example.bo.Person;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * BeanFactory 例子
 */
public class SpringBeanFactoryMain {

    public static void main(String[] args) {
        //這個是BeanFactory
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("application.xml");
        //獲取對應(yīng)的對象化
        Object demo = beanFactory.getBean("demo");
        System.out.println(demo instanceof Person);
        System.out.println(demo);
        //獲取從工廠Bean中獲取對象
        Person demoFromFactory = beanFactory.getBean("demoFromFactory", Person.class);
        System.out.println(demoFromFactory);
        //獲取對應(yīng)的personFactory
        Object bean = beanFactory.getBean("demoFromFactory");
        System.out.println(bean instanceof PersonFactoryBean);
        PersonFactoryBean factoryBean=(PersonFactoryBean) bean;
        System.out.println("初始化參數(shù)為:"+factoryBean.getInitStr());
    }
}

輸出內(nèi)容:

true
Person{name='xiaoMing', age=10}
Person{name='init from factory', age=10}
true
初始化參數(shù)為:10,init from factory

三、主要區(qū)別

  1. BeanFactory:負(fù)責(zé)生產(chǎn)和管理Bean的一個工廠接口,提供一個Spring Ioc容器規(guī)范。
  2. FactoryBean:一種Bean創(chuàng)建的一種方式,對Bean的一種擴展。對于復(fù)雜的Bean對象初始化創(chuàng)建使用其可封裝對象的創(chuàng)建細(xì)節(jié)。

原文鏈接:https://evanwang.blog.csdn.net/article/details/122811855

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新