最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Bean獲取方式的實例化方式詳解

 更新時間:2023年03月09日 11:21:32   作者:@每天都要敲代碼  
工作中需要對一個原本加載屬性文件的工具類修改成對數(shù)據(jù)庫的操作當然,ado層已經(jīng)寫好,但是需要從Spring中獲取bean,然而,工具類并沒有交給Spring來管理,所以需要通過方法獲取所需要的bean。于是整理了Spring獲取bean的幾種方法

Spring為Bean提供了多種實例化方式,通常包括4種方式。(也就是說在Spring中為Bean對象的創(chuàng)建準備了多種方案,目的是:更加靈活)

第一種:通過構造方法實例化

第二種:通過簡單工廠模式實例化

第三種:通過factory-bean實例化(工廠方法模式)

第四種:通過FactoryBean接口實例化

1.通過構造方法實例化

我們之前一直使用的就是這種方式!默認情況下,會調(diào)用Bean的無參數(shù)構造方法,這里在復習一遍!

SpringBean類

package com.bjpowernode.spring.bean;
public class SpringBean {
    public SpringBean() {
        System.out.println("SpringBean的無參數(shù)構造方法執(zhí)行了");
    }
}

spring.xml配置

第一種:在spring配置文件中直接配置類全路徑,Spring會自動調(diào)用該類的無參數(shù)構造方法來實例化Bean!

<?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">
    <!--Spring提供的實例化方式,第一種-->
    <bean id="sb" class="com.bjpowernode.spring.bean.SpringBean"/>
</beans>

BeanInstantiationTest測試類

package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.SpringBean;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
    @Test
    public void tesInstantiation1(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
        System.out.println(sb);
    }
}

執(zhí)行結果:成功調(diào)用無參數(shù)構造方法實例化對象

2.通過簡單工廠模式實例化

簡單工廠模式又叫做靜態(tài)工廠方法模式,因為工廠類中有一個靜態(tài)方法!

第一步:定義一個Bean

package com.bjpowernode.spring.bean;
public class Vip {
    public Vip() {
        System.out.println("我是一個Vip");
    }
}

第二步:編寫簡單工廠模式當中的工廠類

package com.bjpowernode.spring.bean;
public class VipFactory {
    // 里面有一個靜態(tài)方法
    public static Vip get(){
        // 實際上對象的創(chuàng)建還是我們程序員自己完成的
        return new Vip();
    }
}

第三步:在Spring配置文件中指定創(chuàng)建該Bean的方法

第二種:通過簡單工廠模式。

需要在Spring配置文件中告訴Spring框架,調(diào)用哪個類的哪個方法獲取Bean?

①class屬性指定的是工廠類的全限定類名!

②factory-method屬性指定的是工廠類當中的靜態(tài)方法,也就是告訴Spring框架,調(diào)用這個方法可以獲取Bean!

<?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">
    <!--Spring提供的實例化方式,第二種-->
    <bean id="vipBean" class="com.bjpowernode.spring.bean.VipFactory" factory-method="get"/>
</beans>

第四步:編寫測試程序

package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.SpringBean;
import com.bjpowernode.spring.bean.Vip;
import com.bjpowernode.spring.bean.VipFactory;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
    @Test
    public void tesInstantiation2(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Object vipBean = applicationContext.getBean("vipBean", Vip.class);
        System.out.println(vipBean);
    }
}

執(zhí)行結果:通過簡單工廠模式也能實例化對象

3.通過factory-bean實例化

本質(zhì)上是:通過工廠方法模式進行實例化對象!

注:簡單工廠模式和工廠方法模式的區(qū)別

①簡單工廠模式是所有的產(chǎn)品對應一個工廠類,使用的是靜態(tài)方法!

②工廠方法模式是一個產(chǎn)品對應一個工廠類,使用的是實例方法!

第一步:定義一個Bean

package com.bjpowernode.spring.bean;
// 工廠方法模式當中的:具體產(chǎn)品角色
public class Gun {
    public Gun() {
        System.out.println("Gun的無參數(shù)構造方法執(zhí)行");
    }
}

第二步:定義具體工廠類,工廠類中定義實例方法

package com.bjpowernode.spring.bean;
// 工廠方法模式當中:的具體工廠角色
public class GunFactory {
    // 實例方法
    public Gun get(){
        // 還是我們自己new的對象
        return new Gun();
    }
}

第三步:在Spring配置文件中指定factory-bean以及factory-method

第三種:通過工廠方法模式。

通過 factory-bean屬性 + factory-method屬性來共同完成。告訴Spring框架,調(diào)用哪個對象(因為是實例方法需要創(chuàng)建對象)的哪個方法來獲取Bean。

①factory-bean屬性用來告訴Spring調(diào)用那個對象!

②factory-method屬性用來告訴Spring調(diào)用該對象的那個方法!

<?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">
    <!--Spring提供的實例化方式,第三種-->
    <bean id="gunBean" class="com.bjpowernode.spring.bean.GunFactory"/>
    <bean id="gun" factory-bean="gunBean" factory-method="get"/> 
</beans>

第四步:編寫測試程序

package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.Gun;
import com.bjpowernode.spring.bean.SpringBean;
import com.bjpowernode.spring.bean.Vip;
import com.bjpowernode.spring.bean.VipFactory;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
    @Test
    public void tesInstantiation3(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Gun gun = applicationContext.getBean("gun", Gun.class);
        System.out.println(gun);
    }
}

執(zhí)行結果:通過工廠方法模式也能實例化對象

4.通過FactoryBean接口實例化

①在第三種方式中,factory-bean和factory-method都是我們自己定義的。

②在Spring中,當編寫的類直接實現(xiàn)FactoryBean接口之后,factory-bean和factory-method就不需要指定了!factory-bean會自動指向?qū)崿F(xiàn)FactoryBean接口的類,factory-method會自動指向getObject()方法!

第一步:定義一個Bean

package com.bjpowernode.spring.bean;
public class Person {
    public Person() {
        System.out.println("Person的無參數(shù)構造方法執(zhí)行了");
    }
}

第二步:編寫一個類實現(xiàn)FactoryBean接口,重寫里面的方法

PersonFactory也是一個Bean,只不過這個Bean比較特殊,叫做工廠Bean。通過工廠Bean這個特殊的Bean可以獲取一個普通的Bean!

package com.bjpowernode.spring.bean;
import org.springframework.beans.factory.FactoryBean;
public class PersonFactory implements FactoryBean<Person> {
    @Override
    public Person getObject() throws Exception {
        // 對象的創(chuàng)建也是自己new的
        return new Person();
    }
    @Override
    public Class<?> getObjectType() {
        return null;
    }
    @Override
    public boolean isSingleton() {
        // 這個方法是默認存在的,true表示單例,false表示原型
        return true;
    }
}

第三步:在Spring配置文件中配置FactoryBean

第四種:通過FactoryBean接口來實現(xiàn),這種方式實際上就是第三種方式的簡化!

①由于你編寫的類實現(xiàn)了FactoryBean接口,所以這個類是一個特殊的類,不需要你再手動指定:factory-bean、factory-method。 ②通過一個特殊的Bean:工廠Bean,來返回一個普通的Bean Person對象。即通過FactoryBean這個工廠Bean主要是想對普通Bean進行加工處理!

<?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">
    <!--Spring提供的實例化方式,第四種-->
    <bean id="person" class="com.bjpowernode.spring.bean.PersonFactory" />
</beans>

第四步:編寫測試程序

package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.*;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
    @Test
    public void tesInstantiation4(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Person person = applicationContext.getBean("person", Person.class);
        System.out.println(person);
    }
}

執(zhí)行結果:通過FactoryBean接口實例化

注:FactoryBean在Spring中是一個接口,被稱為“工廠Bean”。“工廠Bean”是一種特殊的Bean,所有的“工廠Bean”都是用來協(xié)助Spring框架來創(chuàng)建其他Bean對象的!

5.BeanFactory和FactoryBean的區(qū)別-面試題

(1)BeanFactory(是一個工廠)

BeanFactory是Spring IoC容器的頂級對象,BeanFactory被翻譯為“Bean工廠”,在Spring的IoC容器中,“Bean工廠”負責創(chuàng)建Bean對象!

(2)FactoryBean(是一個Bean)

FactoryBean是一個Bean,是一個能夠輔助Spring實例化其它Bean對象的一個Bean!

在Spring中,Bean可以分為兩類:

  • 第一類:普通Bean
  • 第二類:工廠Bean(工廠Bean也是一種Bean,只不過這種Bean比較特殊,它可以輔助Spring實例化其它Bean對象)

6.使用FactoryBean注入自定義Date

①前面我們說過,java.util.Date在Spring中被當做簡單類型,簡單類型在注入的時候可以直接使用value屬性或value標簽來完成。

②但是之前我們已經(jīng)測試過了,對于Date類型來說,采用value屬性或value標簽賦值的時候,對日期字符串的格式要求非常嚴格,必須是這種格式的:Mon Oct 10 14:30:26 CST 2022,其他格式是不會被識別的!

③當然我們也可以當成非簡單類型處理,使用ref屬性來處理,但是卻有一個弊端,獲取的都是當前的時間,并不能自己指定時間!

注:下面我們就使用FactoryBean來完成這個騷操作!

Student類

package com.bjpowernode.spring.bean;
import java.util.Date;
public class Student {
    // 每個學生都有出生日期
    private Date birth;
    @Override
    public String toString() {
        return "Student{" +
                "birth=" + birth +
                '}';
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
}

編寫DateFactory實現(xiàn)FactoryBean接口

package com.bjpowernode.spring.bean;
import org.springframework.beans.factory.FactoryBean;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFactory implements FactoryBean<Date> {
    // 定義一個日期屬性,用來處理傳過來的日期字符串
    private String date;
    // 通過構造方法給日期字符串屬性賦值
    public DateFactory(String date) {
        this.date = date;
    }
    @Override
    public Date getObject() throws Exception {
        // 處理
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(this.date);
    }
    @Override
    public Class<?> getObjectType() {
        return null;
    }
}

編寫spring配置文件

<?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">
    <!--通過這個類的構造方法,把字符串轉(zhuǎn)換成Date-->
    <bean id="date" class="com.bjpowernode.spring.bean.DateFactory">
        <constructor-arg name="date" value="1999-01-14"/>
    </bean>
    <!--把上面的Date通過上面的類,使用ref屬性引進來-->
    <bean id="studentBean" class="com.bjpowernode.spring.bean.Student">
        <property name="birth" ref="date"/>
    </bean>
</beans>

編寫測試程序

package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.*;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
    @Test
    public void testDate(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Student studentBean = applicationContext.getBean("studentBean", Student.class);
        System.out.println(studentBean);
    }
}

執(zhí)行結果

到此這篇關于Spring Bean獲取方式的實例化方式詳解的文章就介紹到這了,更多相關Spring Bean獲取方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式

    SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式

    這篇文章主要介紹了SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • SpringBoot項目中訪問HTML頁面的三種方法

    SpringBoot項目中訪問HTML頁面的三種方法

    這篇文章主要介紹了SpringBoot項目中訪問HTML頁面的三種方法,文中通過代碼示例和圖文結合的方式講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • spring boot與ktor整合的實現(xiàn)方法

    spring boot與ktor整合的實現(xiàn)方法

    這篇文章主要給大家介紹了關于spring boot與ktor整合的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Java數(shù)據(jù)結構之常見排序算法(下)

    Java數(shù)據(jù)結構之常見排序算法(下)

    這篇文章主要介紹了Java數(shù)據(jù)結構之常見排序算法(下),與之相對有(上),想了解的朋友可以去本網(wǎng)站掃搜,在這兩篇文章里涵蓋關于八大排序算法的所有內(nèi)容,需要的朋友可以參考下
    2023-01-01
  • Spring中的Sentinel規(guī)則持久化解析

    Spring中的Sentinel規(guī)則持久化解析

    這篇文章主要介紹了Spring中的Sentinel規(guī)則持久化解析,具體內(nèi)容包括,Sentinel規(guī)則推送三種模式介紹,原始模式,拉模式,推模式,并對基于Nacos配置中心控制臺實現(xiàn)推送進行詳盡介紹,需要的朋友可以參考下
    2023-09-09
  • MyBatis中?@Mapper?和?@MapperScan?的區(qū)別與使用解析

    MyBatis中?@Mapper?和?@MapperScan?的區(qū)別與使用解析

    本文介紹了SpringBoot中MyBatis的兩個常用注解:@Mapper和@MapperScan,@Mapper用于標記單個Mapper接口,而@MapperScan用于批量掃描指定包下的所有Mapper接口,兩者都有各自適用的場景,選擇合適的注解可以提高開發(fā)效率并使代碼更加簡潔,感興趣的朋友一起看看吧
    2025-01-01
  • freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例

    freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例

    這篇文章主要介紹了freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 分布式服務Dubbo+Zookeeper安全認證實例

    分布式服務Dubbo+Zookeeper安全認證實例

    下面小編就為大家分享一篇分布式服務Dubbo+Zookeeper安全認證實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 解析ConcurrentHashMap: put方法源碼分析

    解析ConcurrentHashMap: put方法源碼分析

    ConcurrentHashMap是由Segment數(shù)組結構和HashEntry數(shù)組結構組成。Segment的結構和HashMap類似,是一種數(shù)組和鏈表結構,今天給大家普及java面試常見問題---ConcurrentHashMap知識,一起看看吧
    2021-06-06
  • 淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類

    淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類

    Spring為大家內(nèi)置了不少開箱即用的轉(zhuǎn)換類,如字符串轉(zhuǎn)數(shù)字、字符串轉(zhuǎn)時間等,但有時候需要使用自定義的屬性,則需要自定義轉(zhuǎn)換類了
    2021-06-06

最新評論

手游| 佛学| 台江县| 合山市| 清涧县| 舟山市| 广宁县| 平安县| 抚远县| 临江市| 大丰市| 岱山县| 新巴尔虎左旗| 昌图县| 原平市| 花莲县| 思南县| 沂水县| 辽阳市| 木里| 三穗县| 临清市| 商都县| 新闻| 白沙| 彭州市| 濉溪县| 轮台县| 德州市| 灵寿县| 黄梅县| 连山| 广饶县| 霍城县| 浦城县| 永寿县| 定远县| 云南省| 满洲里市| 盐亭县| 哈密市|