當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring5 - 向IOC容器中添加组件的4种方式
生活随笔
收集整理的這篇文章主要介紹了
Spring5 - 向IOC容器中添加组件的4种方式
小編覺(jué)得挺不錯(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)單來(lái)說(shuō),4種方式
- @CompentScan + @Controller @Service @Respository @compent等注解
- @Bean
- @Import
- FacotryBean
接下來(lái)我們針對(duì)每種方式,來(lái)演示一下
方式一: @CompentScan
適用場(chǎng)景
一般我們自己寫(xiě)的代碼都是通過(guò)這種方式來(lái)實(shí)現(xiàn)的bean加載到ioc容器中
Code
查考: Spring5源碼 - Spring IOC 注解復(fù)習(xí) @CompentScan 部分
方式二: @Bean
適用場(chǎng)景
通常我們初始化Redis 、數(shù)據(jù)庫(kù)等等,都會(huì)使用這種方式,即 適用于導(dǎo)入第三方組件的類(lèi)
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為類(lèi)的全路徑名
Code Demo1
【components】
package com.artisan.base.importTest.component;public class Bean7 {} package com.artisan.base.importTest.component;public class Bean8 {}【配置類(lèi)】
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);} }【配置類(lèi)】
【測(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;} }【配置類(lèi)】
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èn)題。
- 上一篇: Spring5源码 - Spring I
- 下一篇: Spring5 - Bean的初始化和销