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

Spring單元測試控制Bean注入的方式

 更新時間:2023年04月10日 10:55:37   作者:禿頭披風(fēng)俠.  
這篇文章主要介紹了Spring單元測試控制Bean注入的方式,其中續(xù)注意的是在Bean上加@Order(xxx)是無法控制bean注入的順序的,需要的可以參考一下

通過xml文件進(jìn)行注入

在配置文件中指定要注入的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">
    <bean id="dog" class="com.ttpfx.entity.Dog">
        <property name="name" value="旺財"/>
        <property name="age" value="18"/>
    </bean>
</beans>

然后spring加載這個xml文件就可以實現(xiàn)注入

public class SpringTest1 {
    public static void main(String[] args) {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
    }
}

輸出為

dog

通過xml加注解方式進(jìn)行注入

編寫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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 掃描 com.ttpfx.entity.t2 包下的所有bean-->
    <context:component-scan base-package="com.ttpfx.entity.t2"/>
</beans>

然后在要注入的bean上加入Component注解即可(如果里面方法上面有@Bean,那么也會進(jìn)行處理)

@Component
public class Cat {
}
public class SpringTest2 {
    public static void main(String[] args) {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
    }
}

輸出為

cat
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory

通過注解進(jìn)行注入

可以不使用xml文件,通過@ComponentScan注解來完成定義掃描路徑的功能

@ComponentScan(basePackages = "com.ttpfx.entity.t3")
public class SpringConfig3 {
}
public class SpringTest3 {
    public static void main(String[] args) {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig3.class);
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
    }
}

使用@ComponentScan也會將自身加入到容器中。我們可以在方法上加入@Bean來進(jìn)行注入,具體如下

@Component和@Configuration的區(qū)別

二者用法基本一樣,只不過@Configuration可以控制注入的Bean是不是一個代理對象,如果是代理對象,那么調(diào)用@Bean方法返回的都是同一個對象,否則就不是同一個對象。

在默認(rèn)情況下,@Configuration注入的對象是一個代理對象

默認(rèn)情況,proxyBeanMethods = true

@Configuration(proxyBeanMethods = true)
public class Cat {
    @Bean
    public Cat bigCat() {
        return new Cat();
    }
}

得到這個對象,然后調(diào)用bigCat這個方法

public class SpringTest2 {
    public static void main(String[] args) {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Cat cat = ioc.getBean("cat", Cat.class);
        System.out.println(cat);
        Cat bigCat1 = cat.bigCat();
        Cat bigCat2 = cat.bigCat();
        System.out.println("---------------------");
        System.out.println(bigCat1);
        System.out.println(bigCat2);
        System.out.println(bigCat1 == bigCat2);
    }
}

這時返回Cat已經(jīng)是一個代理對象了,bigCat返回的都是同一個對象,就是單例模式的。

com.ttpfx.entity.t2.Cat$$EnhancerBySpringCGLIB$$bc3ad26b@4c1d9d4b
---------------------
com.ttpfx.entity.t2.Cat@7b227d8d
com.ttpfx.entity.t2.Cat@7b227d8d
true

如果將proxyBeanMethods 改成false,情況如下

@Configuration(proxyBeanMethods = false)
public class Cat {
    @Bean
    public Cat bigCat() {
        return new Cat();
    }
}

其他代碼不變,可以發(fā)現(xiàn)沒有進(jìn)行代理。

com.ttpfx.entity.t2.Cat@62fdb4a6
---------------------
com.ttpfx.entity.t2.Cat@11e21d0e
com.ttpfx.entity.t2.Cat@1dd02175
false

如果使用@Component,那么就相當(dāng)于@Configuration的proxyBeanMethods 設(shè)置為false

使用FactoryBean

我們可以讓一個類實現(xiàn)FactoryBean,這個接口有一個getObject方法,如果一個使用@Bean標(biāo)記的方法返回FactoryBean,那么最終返回的是FactoryBean的getObject方法返回的值

public class PeopleFactory implements FactoryBean<People> {
    @Override
    public People getObject() throws Exception {
        return new People();
    }
    @Override
    public Class<?> getObjectType() {
        return People.class;
    }
    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}
@Component
public class People {
    @Bean
    public PeopleFactory peopleFactory(){
        return new PeopleFactory();
    }
}

此時獲取peopleFactory,它的類型如下,是一個People類型

com.ttpfx.entity.t3.People@587c290d

通過@Import導(dǎo)入

可以使用@Import進(jìn)行導(dǎo)入

@Import({User.class})
public class SpringConfig4 {
}
public class SpringTest4 {
    public static void main(String[] args) {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig4.class);
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
    }
}

輸出如下,可以發(fā)現(xiàn)使用@Import標(biāo)注的類也會被注入。使用@Import導(dǎo)入的類,名稱為全類名,如果重復(fù)導(dǎo)入,那么后面覆蓋前面。要指定名稱,那么就在對應(yīng)的bean上面使用@Component即可

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig4
com.ttpfx.entity.t4.User

手動注入(registerBean)

可以直接通過GenericApplicationContext這個類的registerBean方法進(jìn)行注入

public class SpringTest5 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig5.class);
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("-----------------------");
        ioc.registerBean("monster01", Monster.class);
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
    }
}

輸出如下

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig5
-----------------------
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig5
monster01

通過ImportSelector進(jìn)行注入

定義一個類實現(xiàn)ImportSelector

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.ttpfx.entity.t6.Pig"};
    }
}
@Import({MyImportSelector.class})
public class SpringConfig6 {
}
public class SpringTest6 {
    public static void main(String[] args) {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig6.class);
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
    }
}

輸出如下,可以發(fā)現(xiàn)selectImports返回的String字符串中的內(nèi)容會進(jìn)行注入

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig6
com.ttpfx.entity.t6.Pig

通過ImportBeanDefinitionRegistrar進(jìn)行注入

通過ImportBeanDefinitionRegistrar可以進(jìn)行注入,只需要在registerBeanDefinitions方法中使用BeanDefinitionRegistry的registerBeanDefinition方法即可

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
        ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry, importBeanNameGenerator);
    }
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Manager.class).getBeanDefinition();
        registry.registerBeanDefinition("manager", beanDefinition);
        ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry);
    }
}
public class SpringTest7 {
    public static void main(String[] args) {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig7.class);
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
        Manager bean = ioc.getBean(Manager.class);
        System.out.println(bean);
    }
}

代碼輸出如下

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig7
manager
com.ttpfx.entity.t7.Manager@1d8d30f7

通過BeanDefinitionRegistryPostProcessor進(jìn)行注入

實現(xiàn)這個接口,通過方法上面的參數(shù)可以進(jìn)行注入

public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Employee.class).getBeanDefinition();
        registry.registerBeanDefinition("employee", beanDefinition);
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    }
}
@Import({MyBeanDefinitionRegistryPostProcessor.class})
public class SpringConfig8 {
}
public class SpringTest8 {
    public static void main(String[] args) {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig8.class);
        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("-----------------");
        Employee bean = ioc.getBean(Employee.class);
        System.out.println(bean);
    }
}

輸出如下,可以發(fā)現(xiàn)實現(xiàn)BeanDefinitionRegistryPostProcessor的這個類也被注入了

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig8
com.ttpfx.entity.t8.MyBeanDefinitionRegistryPostProcessor
employee
-----------------
com.ttpfx.entity.t8.Employee@58c1670b

到此這篇關(guān)于Spring注入bean的方式詳細(xì)講解的文章就介紹到這了,更多相關(guān)Spring注入bean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot前后臺數(shù)據(jù)交互的示例代碼

    springboot前后臺數(shù)據(jù)交互的示例代碼

    這篇文章主要介紹了springboot前后臺數(shù)據(jù)交互的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 深入了解SparkSQL的運用及方法

    深入了解SparkSQL的運用及方法

    SparkSQL就是將SQL轉(zhuǎn)換成一個任務(wù),提交到集群上運行,類似于Hive的執(zhí)行方式。本文給大家分享了SparkSQl的運用及方法,感興趣的朋友跟隨小編一起看看吧
    2022-03-03
  • RocketMQ中的消費模式和消費策略詳解

    RocketMQ中的消費模式和消費策略詳解

    這篇文章主要介紹了RocketMQ中的消費模式和消費策略詳解,RocketMQ 是基于發(fā)布訂閱模型的消息中間件,所謂的發(fā)布訂閱就是說,consumer 訂閱了 broker 上的某個 topic,當(dāng) producer 發(fā)布消息到 broker 上的該 topic 時,consumer 就能收到該條消息,需要的朋友可以參考下
    2023-10-10
  • Java設(shè)計模式之備忘錄模式

    Java設(shè)計模式之備忘錄模式

    這篇文章主要介紹了Java設(shè)計模式之備忘錄模式,備忘錄模式(Memento Pattern),屬于行為型設(shè)計模式,目的是用于保存一個對象在某一時刻的狀態(tài),以便于在將來某個時刻根據(jù)此狀態(tài)恢復(fù)該對象,需要的朋友可以參考下
    2023-12-12
  • Spring Boot異步調(diào)用@Async過程詳解

    Spring Boot異步調(diào)用@Async過程詳解

    這篇文章主要介紹了Spring Boot異步調(diào)用@Async過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • jmeter的時間戳函數(shù)使用

    jmeter的時間戳函數(shù)使用

    在使用jmeter做接口測試的時候,經(jīng)常會要用到日期這種函數(shù),本文主要介紹了jmeter的時間戳函數(shù)使用,感興趣的可以了解一下
    2021-11-11
  • Spring Boot不同版本Redis設(shè)置JedisConnectionFactory詳解

    Spring Boot不同版本Redis設(shè)置JedisConnectionFactory詳解

    本文章向大家介紹Spring Boot不同版本Redis設(shè)置JedisConnectionFactory,主要內(nèi)容包括1.X 版本、2.X 版本、2.、基本概念、基礎(chǔ)應(yīng)用、原理機(jī)制和需要注意的事項等,并結(jié)合實例形式分析了其使用技巧,希望通過本文能幫助到大家理解應(yīng)用這部分內(nèi)容
    2023-09-09
  • 實例講解MyBatis如何防止SQL注入

    實例講解MyBatis如何防止SQL注入

    這篇文章通過實例代碼介紹MyBatis如何防止SQL注入,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • Java實現(xiàn)圖片上傳到服務(wù)器并把上傳的圖片讀取出來

    Java實現(xiàn)圖片上傳到服務(wù)器并把上傳的圖片讀取出來

    在各大網(wǎng)站上都可以實現(xiàn)上傳頭像功能,可以選擇自己喜歡的圖片做頭像,從本地上傳,今天小編給大家分享Java實現(xiàn)圖片上傳到服務(wù)器并把上傳的圖片讀取出來,需要的朋友參考下
    2017-02-02
  • Java無限級樹(遞歸)超實用案例

    Java無限級樹(遞歸)超實用案例

    下面小編就為大家?guī)硪黄狫ava無限級樹(遞歸)超實用案例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11

最新評論

改则县| 边坝县| 汶川县| 泾川县| 聂拉木县| 淅川县| 九龙坡区| 巩留县| 拉萨市| 宿迁市| 泽库县| 芜湖市| 达尔| 当涂县| 吉木乃县| 潼关县| 鄂尔多斯市| 开封市| 蕉岭县| 临颍县| 舟曲县| 马尔康县| 交城县| 永修县| 霍州市| 凌源市| 宿州市| 游戏| 伊吾县| 禄丰县| 汉寿县| 织金县| 黎平县| 江门市| 盘锦市| 平舆县| 吉林省| 永嘉县| 九江市| 遵义县| 马鞍山市|