前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Spring5 - 向IOC容器中添加组件的4种方式

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

作者头像
小小工匠
发布2021-08-17 15:56:05
发布2021-08-17 15:56:05
1.1K00
代码可运行
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构
运行总次数:0
代码可运行

概述

简单来说,4种方式

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

接下来我们针对每种方式,来演示一下


方式一: @CompentScan

适用场景

一般我们自己写的代码都是通过这种方式来实现的bean加载到ioc容器中

Code

查考: Spring5源码 - Spring IOC 注解复习 @CompentScan 部分


方式二: @Bean

适用场景

通常我们初始化Redis 、数据库等等,都会使用这种方式,即 适用于导入第三方组件的类


Code

举个例子

代码语言:javascript
代码运行次数:0
运行
复制
 @Bean
    public JedisPool redisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        return jedisPool;
    }

方式三: @Import

适用场景

第三方的组件 可以使用这种方式

导入的组件的id为类的全路径名


Code Demo1

【components】

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.importTest.component;

public class Bean7 {
 
}
代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.importTest.component;

public class Bean8 {



}

【配置类】

代码语言:javascript
代码运行次数:0
运行
复制
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 {
 
}

【验证】

代码语言:javascript
代码运行次数:0
运行
复制
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 + 实现 ImportSelector接口

【自定義ImportSelector】

代码语言:javascript
代码运行次数:0
运行
复制
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 {
	@Override
	public String[] selectImports(AnnotationMetadata importingClassMetadata) {
		return new String[]{"com.artisan.base.importTest.component.Bean6666"};
	}
}

【测试】


Code Demo3 + 实现 ImportBeanDefinitionRegistrar接口

代码语言:javascript
代码运行次数:0
运行
复制
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  {
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Bean7777.class);
		registry.registerBeanDefinition("bean7777",rootBeanDefinition);
	}
}

【配置类】

【测试结果】


方式四 FacotryBean

适用场景

比如整合第三方框架,MyBatis

Spring5源码 - 08 BeanFactory和FactoryBean 源码解析 & 使用场景


Code

【FactoryBean】

代码语言:javascript
代码运行次数:0
运行
复制
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 {
	@Override
	public Object getObject() throws Exception {
		return new Bean8();
	}

	@Override
	public Class<?> getObjectType() {
		return Bean8.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}
}

【配置类】

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.factoryBean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FBConfig {

	// 实例化ArtisanFactoryBean
	@Bean  
	public ArtisanFactoryBean artisanFactoryBean() {
		return new ArtisanFactoryBean();
	}

}

【pojo】

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.factoryBean;


public class Bean8 {

	public Bean8() {

		System.out.println("Bean8 Create");
	}
}

【测试】

代码语言:javascript
代码运行次数:0
运行
复制
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("=========================");
		// 调用FactoryBean的getObject方法
		System.out.println(ac.getBean("artisanFactoryBean"));

		// & 获取FactoryBean本身
		System.out.println(ac.getBean("&artisanFactoryBean"));

	}
}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/10/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 方式一: @CompentScan
    • 适用场景
    • Code
  • 方式二: @Bean
    • 适用场景
    • Code
  • 方式三: @Import
    • 适用场景
    • Code Demo1
    • Code Demo2 + 实现 ImportSelector接口
    • Code Demo3 + 实现 ImportBeanDefinitionRegistrar接口
  • 方式四 FacotryBean
    • 适用场景
    • Code
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档