亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring?Bean獲取方式的實(shí)例化方式詳解

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

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

第一種:通過(guò)構(gòu)造方法實(shí)例化

第二種:通過(guò)簡(jiǎn)單工廠模式實(shí)例化

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

第四種:通過(guò)FactoryBean接口實(shí)例化

1.通過(guò)構(gòu)造方法實(shí)例化

我們之前一直使用的就是這種方式!默認(rèn)情況下,會(huì)調(diào)用Bean的無(wú)參數(shù)構(gòu)造方法,這里在復(fù)習(xí)一遍!

SpringBean類

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

spring.xml配置

第一種:在spring配置文件中直接配置類全路徑,Spring會(huì)自動(dòng)調(diào)用該類的無(wú)參數(shù)構(gòu)造方法來(lái)實(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提供的實(shí)例化方式,第一種-->
    <bean id="sb" class="com.bjpowernode.spring.bean.SpringBean"/>
</beans>

BeanInstantiationTest測(cè)試類

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í)行結(jié)果:成功調(diào)用無(wú)參數(shù)構(gòu)造方法實(shí)例化對(duì)象

2.通過(guò)簡(jiǎn)單工廠模式實(shí)例化

簡(jiǎn)單工廠模式又叫做靜態(tài)工廠方法模式,因?yàn)楣S類中有一個(gè)靜態(tài)方法!

第一步:定義一個(gè)Bean

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

第二步:編寫簡(jiǎn)單工廠模式當(dāng)中的工廠類

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

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

第二種:通過(guò)簡(jiǎn)單工廠模式。

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

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

②factory-method屬性指定的是工廠類當(dāng)中的靜態(tài)方法,也就是告訴Spring框架,調(diào)用這個(gè)方法可以獲取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提供的實(shí)例化方式,第二種-->
    <bean id="vipBean" class="com.bjpowernode.spring.bean.VipFactory" factory-method="get"/>
</beans>

第四步:編寫測(cè)試程序

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í)行結(jié)果:通過(guò)簡(jiǎn)單工廠模式也能實(shí)例化對(duì)象

3.通過(guò)factory-bean實(shí)例化

本質(zhì)上是:通過(guò)工廠方法模式進(jìn)行實(shí)例化對(duì)象!

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

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

②工廠方法模式是一個(gè)產(chǎn)品對(duì)應(yīng)一個(gè)工廠類,使用的是實(shí)例方法!

第一步:定義一個(gè)Bean

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

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

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

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

第三種:通過(guò)工廠方法模式。

通過(guò) factory-bean屬性 + factory-method屬性來(lái)共同完成。告訴Spring框架,調(diào)用哪個(gè)對(duì)象(因?yàn)槭菍?shí)例方法需要?jiǎng)?chuàng)建對(duì)象)的哪個(gè)方法來(lái)獲取Bean。

①factory-bean屬性用來(lái)告訴Spring調(diào)用那個(gè)對(duì)象!

②factory-method屬性用來(lái)告訴Spring調(diào)用該對(duì)象的那個(gè)方法!

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

第四步:編寫測(cè)試程序

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í)行結(jié)果:通過(guò)工廠方法模式也能實(shí)例化對(duì)象

4.通過(guò)FactoryBean接口實(shí)例化

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

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

第一步:定義一個(gè)Bean

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

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

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

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

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

第四種:通過(guò)FactoryBean接口來(lái)實(shí)現(xiàn),這種方式實(shí)際上就是第三種方式的簡(jiǎn)化!

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

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

第四步:編寫測(cè)試程序

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í)行結(jié)果:通過(guò)FactoryBean接口實(shí)例化

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

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

(1)BeanFactory(是一個(gè)工廠)

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

(2)FactoryBean(是一個(gè)Bean)

FactoryBean是一個(gè)Bean,是一個(gè)能夠輔助Spring實(shí)例化其它Bean對(duì)象的一個(gè)Bean!

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

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

6.使用FactoryBean注入自定義Date

①前面我們說(shuō)過(guò),java.util.Date在Spring中被當(dāng)做簡(jiǎn)單類型,簡(jiǎn)單類型在注入的時(shí)候可以直接使用value屬性或value標(biāo)簽來(lái)完成。

②但是之前我們已經(jīng)測(cè)試過(guò)了,對(duì)于Date類型來(lái)說(shuō),采用value屬性或value標(biāo)簽賦值的時(shí)候,對(duì)日期字符串的格式要求非常嚴(yán)格,必須是這種格式的:Mon Oct 10 14:30:26 CST 2022,其他格式是不會(huì)被識(shí)別的!

③當(dāng)然我們也可以當(dāng)成非簡(jiǎn)單類型處理,使用ref屬性來(lái)處理,但是卻有一個(gè)弊端,獲取的都是當(dāng)前的時(shí)間,并不能自己指定時(shí)間!

注:下面我們就使用FactoryBean來(lái)完成這個(gè)騷操作!

Student類

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

編寫DateFactory實(shí)現(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> {
    // 定義一個(gè)日期屬性,用來(lái)處理傳過(guò)來(lái)的日期字符串
    private String date;
    // 通過(guò)構(gòu)造方法給日期字符串屬性賦值
    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">
    <!--通過(guò)這個(gè)類的構(gòu)造方法,把字符串轉(zhuǎn)換成Date-->
    <bean id="date" class="com.bjpowernode.spring.bean.DateFactory">
        <constructor-arg name="date" value="1999-01-14"/>
    </bean>
    <!--把上面的Date通過(guò)上面的類,使用ref屬性引進(jìn)來(lái)-->
    <bean id="studentBean" class="com.bjpowernode.spring.bean.Student">
        <property name="birth" ref="date"/>
    </bean>
</beans>

編寫測(cè)試程序

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í)行結(jié)果

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

相關(guān)文章

  • 老生常談Java?網(wǎng)絡(luò)編程?——?Socket?詳解

    老生常談Java?網(wǎng)絡(luò)編程?——?Socket?詳解

    這篇文章主要介紹了Java?網(wǎng)絡(luò)編程?——?Socket?相關(guān)知識(shí),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • java算法入門之有效的括號(hào)刪除有序數(shù)組中的重復(fù)項(xiàng)實(shí)現(xiàn)strStr

    java算法入門之有效的括號(hào)刪除有序數(shù)組中的重復(fù)項(xiàng)實(shí)現(xiàn)strStr

    大家好,我是哪吒,一個(gè)熱愛(ài)編碼的Java工程師,本著"欲速則不達(dá),欲達(dá)則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長(zhǎng),所謂成長(zhǎng),不過(guò)是用時(shí)間慢慢擦亮你的眼睛,少時(shí)看重的,年長(zhǎng)后卻視若鴻毛,少時(shí)看輕的,年長(zhǎng)后卻視若泰山,成長(zhǎng)之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程
    2021-08-08
  • Java類的繼承原理與用法分析

    Java類的繼承原理與用法分析

    這篇文章主要介紹了Java類的繼承原理與用法,結(jié)合實(shí)例形式分析了java類的繼承相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐記錄

    SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐記錄

    這篇文章主要介紹了SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • 關(guān)于Spring?Ioc和DI注解的問(wèn)題

    關(guān)于Spring?Ioc和DI注解的問(wèn)題

    這篇文章主要介紹了Spring?Ioc和DI注解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Java持久化XML文件配置解析

    Java持久化XML文件配置解析

    這篇文章主要為大家介紹了Java持久化XML文件配置解析,當(dāng)你在使用?Java?編程語(yǔ)言?來(lái)編寫軟件時(shí),實(shí)現(xiàn)持久化配置的方式。有需要的朋友可以借鑒參考下,希望能夠有所幫助<BR>
    2022-03-03
  • Spring-Cloud-Function-Spel?漏洞環(huán)境搭建

    Spring-Cloud-Function-Spel?漏洞環(huán)境搭建

    這篇文章主要介紹了Spring-Cloud-Function-Spel?漏洞復(fù)現(xiàn)及搭建方法,搭建方法也很簡(jiǎn)單,首先需要安裝maven jdk,具體安裝過(guò)程跟隨小編一起看看吧
    2022-03-03
  • 如何動(dòng)態(tài)替換Spring容器中的Bean

    如何動(dòng)態(tài)替換Spring容器中的Bean

    這篇文章主要介紹了如何動(dòng)態(tài)替換Spring容器中的Bean,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java中的Set、List、Map的用法與區(qū)別介紹

    Java中的Set、List、Map的用法與區(qū)別介紹

    這篇文章主要介紹了Java中的Set、List、Map的用法與區(qū)別,需要的朋友可以參考下
    2016-06-06
  • Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解

    Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解

    這篇文章主要為大家介紹了Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08

最新評(píng)論