SpringBoot注册组件之@Import@ImportResource@ComponentScan
除了上文提到的@Configuration和@Bean注册第三方组件的方式外还有如下方式
- @Import注解
- 优缺点/场景
- 使用
- 示例
- @ImportResource注解
- 优缺点/场景
- 使用
- 示例
- @ComponentScan注解
- 优缺点/场景
- 使用
- 示例
@Import注解
优缺点/场景
- 优点:使用方便
- 缺点:不能定制化属性值等;需要提供无参构造器
使用
- 在容器组件类上使用@Import注解
- 注册组件名全类名
示例
- 待注册对象
package com.ultra.config;public class User {}
- 注册类
package com.ultra.config;import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;@Configuration
@Import({User.class})
public class UserConfig {}
- 测试
package com.ultra;import com.ultra.config.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;/*** @author admin*/
@SpringBootApplication
public class SpringbootWebApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootWebApplication.class, args);String[] names = applicationContext.getBeanNamesForType(User.class);System.out.println("user:" + names[0]);}
}
- 测试结果
@ImportResource注解
优缺点/场景
- 优点:已存在spring bean配置文件,可以一次性注册
使用
- 在容器组件类上使用@ImportResource注解
示例
- 待注册对象
package com.ultra.config;public class User {}
- 注册类
package com.ultra.config;import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;@Configuration
@Import({User.class})
@ImportResource("classpath:spring-bean.xml")
public class UserConfig {}
- spring 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="userFromXml" class="com.ultra.config.User"/>
</beans>
- 测试
package com.ultra;import com.ultra.config.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;/*** @author admin*/
@SpringBootApplication
public class SpringbootWebApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootWebApplication.class, args);String[] names = applicationContext.getBeanNamesForType(User.class);System.out.println("user:" + Arrays.toString(names));}
}
- 测试结果
@ComponentScan注解
优缺点/场景
- 优点:非默认扫描包使用了spring注解注册组件,可以一次性注册
- 默认扫描包:SpringBoot启动类同级及其子级是默认扫描包路径
使用
- 在容器组件类上使用@ComponentScan注解
示例
- 待扫描注册对象
package com.other;import org.springframework.stereotype.Component;@Component
public class OtherUser {}
- 启动类及测试
package com.ultra;import com.other.OtherUser;
import com.ultra.config.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;import java.util.Arrays;/*** @author admin*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.ultra", "com.other"})
public class SpringbootWebApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootWebApplication.class, args);String[] names = applicationContext.getBeanNamesForType(User.class);System.out.println("user:" + Arrays.toString(names));String[] names1 = applicationContext.getBeanNamesForType(OtherUser.class);System.out.println("user:" + names1[0]);}}
- 测试结果
SpringBoot注册组件之@Import@ImportResource@ComponentScan相关推荐
- factorybean 代理类不能按照类型注入_Spring注解驱动开发之四——@Import快速导入组件、FactoryBean 定义工厂注册组件...
本文包含以下内容: @Import快速导入组件 配合ImportSelector 导入组件 配合ImportBeanDefinitionRegistrar 导入组件 FactoryBean 定义工厂注 ...
- spirng4.0-@Conditional 按条件注册bean、@Import导入组件bean、@FactoryBean spring的bean工厂注册bean
一.组件说明 @Conditional({xxx.class, xxx.class}),可用在方法和类上面 按照一定的条件进行判断,把满足条件的bean注册到springIOC容器中. @Import ...
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- Spring注解驱动之注册组件(spring的再回顾)
一. 组件注册 1. 给容器中注册组件 xml方式 创建一个实体类(构造方法等省略) public class Person {private String name;private Integer ...
- spring5学习系列之------1 给容器注册组件一 @Configuration @Bean @Scope @Lazy
在工作中我们对spring已经很常用了,但是用的大部分都是spring一些很少的部分,该系列是基于spring5版本的,先介绍相关组件的内容,在涉及到源码内容.话不多说,直接上干货吧 常用注解之给容器 ...
- 给容器中注册组件 || @Scope -- @Lazy -- @Conditional({Condition}) -- @Import--使用Spring提供的 FactoryBean
* @Scope:调整作用域 * prototype:多实例的:ioc容器启动并不会去调用方法创建对象放在容器中. * 每次获取的时候才会调用方法创建对象: ...
- vue笔记(四)注册组件,路由,vuex
官网 一.项目中的组件注册 二.路由 三.vuex 一.项目中的组件注册 1. 全局 import Loading from '@/components/loading';//封装的loading组件 ...
- SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解
SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解 SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller ...
- 2、组件注册-@Configuration@Bean给容器中注册组件
2.组件注册-@Configuration&@Bean给容器中注册组件 2.1 创建maven项目 spring-annotation pom.xml文件添加 spring-context 依 ...
- render注册一个链接组件_详解vue 动态加载并注册组件且通过 render动态创建该组件...
基于 iview Tabs 组件实现 功能:为每个 tab 动态创建不同的.特定的组件内容,而不需要大量的 import 组件并进行 component 注册 Index.vue import loa ...
最新文章
- SQL Server DB Link相关
- A Grain of Sand 一粒沙子
- 位运算详解+竞赛常见用法总结
- GDLiveStreaming视频推送RTMP
- Qt CheckBox的使用
- mybatis-plus自动生成文件
- 机器人学习--Carnegie Mellon University 认知机器人学课程
- 数据结构与算法之反转单向链表和双向链表
- 手动建立makefile简单实例解析
- 痞子衡嵌入式:如果你正在量产i.MX RT产品,不妨试试这款神器RT-Flash
- 用protues作RC桥式振荡电路仿真,无法形成正弦波,求解惑
- python如何导入datetime包
- ansible:变量调用set_fact和register
- 精密光学测量1-概论
- navicat连接数据库出错,但是SQLyog可以正常连接
- SAP EWM仓储管理系统与自动化仓储硬件对接方案介绍
- 用vivo手机拍照一定要先打开这个设置,不然白浪费这么强大的手机
- 一键还原涂鸦图片_如何用Photoshop清除照片上的涂鸦,恢复原始图像?
- CSDN页面打印不正常的解决方法
- 基于MATLAB的图像压缩感知 算法的实现
热门文章
- Labview学习笔记(一)
- Go 语言为Fibonacci函数实现Read方法
- 【BZOJ1226】学校食堂(动态规划,状态压缩)
- spring 多线程 写入数据库 和 写入 xml文件
- 移动 web 1px 边框解决方案
- [Linux] 学习笔记之安装学习环境(sshd, lrzsz)
- ASP.NET 实践:锁定 ASP.NET 配置设定
- HCIE-Security Day1:防火墙概述、实验环境搭建、三种方式管理防火墙
- vue 鼠标点击事件_点击鼠标,利用VBA代码实现精准控制触发事件的第二方案
- GitLab+Nornor3.0.0完成CI/CD流水线配置(更新版)