文章目录

  • 概述
  • 方式一: @CompentScan
    • 适用场景
    • Code
  • 方式二: @Bean
    • 适用场景
    • Code
  • 方式三: @Import
    • 适用场景
    • Code Demo1
    • Code Demo2 + 实现 ImportSelector接口
    • Code Demo3 + 实现 ImportBeanDefinitionRegistrar接口
  • 方式四 FacotryBean
    • 适用场景
    • Code

概述

简单来说,4种方式

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

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


方式一: @CompentScan

适用场景

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

Code

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


方式二: @Bean

适用场景

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


Code

举个例子

 @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

适用场景

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

导入的组件的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 {}

【验证】

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】

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"};}
}


【测试】


Code Demo3 + 实现 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);}
}

【配置类】

【测试结果】


方式四 FacotryBean

适用场景

比如整合第三方框架,MyBatis

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


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 {// 实例化ArtisanFactoryBean@Bean  public ArtisanFactoryBean artisanFactoryBean() {return new ArtisanFactoryBean();}}

【pojo】

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

【测试】

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"));}
}


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

  1. 六、spring之通过FactoryBean为ioc容器中添加组件

    前面我们已经介绍了几种为容器中添加组件的方法,今天一起学习通过FactoryBean添加组件的方法. 首先我们准备一个类,也就是我们需要注册进spring的ioc容器中的类 类Color: // 不必 ...

  2. 往IOC 容器中添加组件的方式

    通过@CompentScan +@Controller @Service @Respository @compent 适用场景: 针对我们自己写的组件可以通过该方式来进行加载到容器中. 通过@Bean ...

  3. 【小家Spring】Spring注解驱动开发---向Spring Ioc容器中注册Bean的7种方式

    每篇一句 比你有钱的人一定会比你努力,而比你努力的人终有一天会比你有钱 前言 Spring是一个非常强大的反转控制(IOC)框架,以帮助分离项目组件之间的依赖关系.因此可以说Spring容器对Bean ...

  4. java jframe添加面板_JFrame添加组件的两种方式

    对JFrame添加组件有两种方式:1) 用getContentPane()方法获得JFrame的内容面板,再对其加入组件:frame.getContentPane().add(childCompont ...

  5. IOC容器创建bean对象的4种方式

    前言: Spring容器创建bean对象,一般通过反射机制查找bean元素的class属性值来找到要实例化的类,从而实例化bean对象.这便是调用构造方法来实例化bean对象 在某些情况下,若采用简单 ...

  6. 向linux kernel中添加cmdline的四种方式

    cmdline 1. 在dts中的bootargs中添加 2.在BoardConfig中添加 3.在uboot中添加 4.在android的Makefile中添加 ★★★ 友情链接 : 个人博客导读首 ...

  7. Lumen开发:如何向 IoC 容器中添加自己定义的类

    版权声明:本文为博主原创文章,未经博主允许不得转载. 先在起始文件bootstrap/app.php加上$app->register(App\Providers\User\UserService ...

  8. 向Word文件里的表格中添加序号的几种方式

    问题概述   本周遇到两次同样的问题,需要向Word 2007中的表格中插入序号,由于有很多行,手动一行一行的输入序号太麻烦,因此最好能够自动添加序号.第一次的时候我在Word中找到了插入序号的功能, ...

  9. 往JScrollPane中添加组件时滚动条不出现的解决方法

    往滚动条容器里添加容器,再往容器中添加组件,,当组件的大小超过滚动条的大小时,滚动条还不显现的解决办法. 滚动条容器,,在NEW的时候只能传一个容器进去..并且这个容器(zPanel)不需要设置大小. ...

最新文章

  1. 初学者编写python用什么软件好_初学者编写python用什么软件
  2. 澎湖县地产泡沫的破灭
  3. C# 打开指定的文件夹 记住路径中 / 与 \ 的用法
  4. python redis 哨兵_Redis哨兵机制
  5. pyotherside 试用
  6. css-bootstrap的安装与使用
  7. thinkpadx1mdt 网络启动_联想ThinkPad X1 Carbon 2020如何进入bios设置从U盘启动?
  8. BZOJ2325[ZJOI2011]道馆之战——树链剖分+线段树
  9. XGen 苹果IOS神器一键新机改串清理超级全息备份支持IOS789超IGV8使用分享(企鹅290093670)
  10. gis与一般计算机应用系统有哪些异同,地理信息系统概论课后习题全部答案 黄杏元著...
  11. chipgenius芯片精灵v4|chipgenius芯片精灵 usb检测工具绿色版v4.00.1024下载
  12. 计算机word图表布布局在哪,word中的页面布局在哪里
  13. mysql 中的any_value 函数
  14. 怎么做期货可以每天都赚钱?都有哪些技巧和方法?
  15. 以业务改进为目标的流程优化方法
  16. 【案例】avi文件恢复
  17. 苹果推出新款iPad Air和iPad mini,升级A12处理器 1
  18. 数控加工仿真系统的使用介绍(下)
  19. 安卓工作室android studio 美化 ,设置背景图片。
  20. SketchUp二次开发模块介绍及使用

热门文章

  1. 基于PHP的图片共享网站设计,基于php实现的web图片共享系统(论文+程序)
  2. 全球变暖java_全球变暖 蓝桥杯
  3. 二叉树的先序遍历和非递归遍历
  4. 关于计算机应用技术的周记,计算机应用技术专业实习周记范文
  5. 2-spark学习笔记-spark发展概述
  6. python 文件处理1:将某一目录下的文件合并
  7. 【量化交易】组合优化三部曲:换手率和alpha模型换手约束下的最优模型时变IC下的多空/多头最优组合换手率
  8. 职业大揭秘,算法攻城狮在日常工作中都干了些啥?
  9. 牛顿-拉夫逊法进行潮流计算matlab源程序
  10. 基于云计算的海量数据挖掘