日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring5 - 向IOC容器中添加组件的4种方式

發(fā)布時(shí)間:2025/3/21 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring5 - 向IOC容器中添加组件的4种方式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 概述
  • 方式一: @CompentScan
    • 適用場(chǎng)景
    • Code
  • 方式二: @Bean
    • 適用場(chǎng)景
    • Code
  • 方式三: @Import
    • 適用場(chǎng)景
    • Code Demo1
    • Code Demo2 + 實(shí)現(xiàn) ImportSelector接口
    • Code Demo3 + 實(shí)現(xiàn) ImportBeanDefinitionRegistrar接口
  • 方式四 FacotryBean
    • 適用場(chǎng)景
    • Code

概述

簡(jiǎn)單來說,4種方式

  • @CompentScan + @Controller @Service @Respository @compent等注解
  • @Bean
  • @Import
  • FacotryBean

接下來我們針對(duì)每種方式,來演示一下


方式一: @CompentScan

適用場(chǎng)景

一般我們自己寫的代碼都是通過這種方式來實(shí)現(xiàn)的bean加載到ioc容器中

Code

查考: Spring5源碼 - Spring IOC 注解復(fù)習(xí) @CompentScan 部分


方式二: @Bean

適用場(chǎng)景

通常我們初始化Redis 、數(shù)據(jù)庫等等,都會(huì)使用這種方式,即 適用于導(dǎo)入第三方組件的類


Code

舉個(gè)例子

@Beanpublic JedisPool redisPoolFactory() {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxIdle(maxIdle);jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);return jedisPool;}

方式三: @Import

適用場(chǎng)景

第三方的組件 可以使用這種方式

導(dǎo)入的組件的id為類的全路徑名


Code Demo1

【components】

package com.artisan.base.importTest.component;public class Bean7 {} package com.artisan.base.importTest.component;public class Bean8 {}

【配置類】

package com.artisan.base.importTest.config;import com.artisan.base.importTest.component.Bean7; import com.artisan.base.importTest.component.Bean8; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import;@Configuration @Import(value = {Bean7.class, Bean8.class}) public class IMPConfig {}

【驗(yàn)證】

package com.artisan.base.importTest;import com.artisan.base.importTest.config.IMPConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2020/10/11 19:05* @mark: show me the code , change the world*/ public class Test {public static void main(String[] args) {AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(IMPConfig.class);for (String beanDefinitionName : ac.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}System.out.println("========================");// beanname為全路徑名System.out.println(ac.getBean("com.artisan.base.importTest.component.Bean7"));} }


Code Demo2 + 實(shí)現(xiàn) ImportSelector接口

【自定義ImportSelector】

package com.artisan.base.importTest.importSelector;import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata;/*** @author 小工匠* @version 1.0* @description:* @date 2020/10/11 19:20* @mark: show me the code , change the world*/ public class ArtisanImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{"com.artisan.base.importTest.component.Bean6666"};} }


【測(cè)試】


Code Demo3 + 實(shí)現(xiàn) ImportBeanDefinitionRegistrar接口

package com.artisan.base.importTest.importSelector;import com.artisan.base.importTest.component.Bean7777; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2020/10/11 19:26* @mark: show me the code , change the world*/ public class ArtisanBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Bean7777.class);registry.registerBeanDefinition("bean7777",rootBeanDefinition);} }

【配置類】

【測(cè)試結(jié)果】


方式四 FacotryBean

適用場(chǎng)景

比如整合第三方框架,MyBatis

Spring5源碼 - 08 BeanFactory和FactoryBean 源碼解析 & 使用場(chǎng)景


Code

【FactoryBean】

package com.artisan.base.factoryBean;import org.springframework.beans.factory.FactoryBean;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2020/10/11 21:49* @mark: show me the code , change the world*/ public class ArtisanFactoryBean implements FactoryBean {@Overridepublic Object getObject() throws Exception {return new Bean8();}@Overridepublic Class<?> getObjectType() {return Bean8.class;}@Overridepublic boolean isSingleton() {return true;} }

【配置類】

package com.artisan.base.factoryBean;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class FBConfig {// 實(shí)例化ArtisanFactoryBean@Bean public ArtisanFactoryBean artisanFactoryBean() {return new ArtisanFactoryBean();}}

【pojo】

package com.artisan.base.factoryBean;public class Bean8 {public Bean8() {System.out.println("Bean8 Create");} }

【測(cè)試】

package com.artisan.base.factoryBean;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {public static void main(String[] args) {AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(FBConfig.class);System.out.println("=========================");// 調(diào)用FactoryBean的getObject方法System.out.println(ac.getBean("artisanFactoryBean"));// & 獲取FactoryBean本身System.out.println(ac.getBean("&artisanFactoryBean"));} }


總結(jié)

以上是生活随笔為你收集整理的Spring5 - 向IOC容器中添加组件的4种方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。