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 ...
最新文章
- Tomcat无法访问中文路径的解决办法
- 泛型的作用是什么?——Java系列学习笔记
- Django(part29)--Q对象
- 关于python测试webservice接口的视频分享
- 第一章MCS-51单片机结构,单片机原理、接口及应用
- 吴军信息论40讲_吴军:有时候成功比失败更糟糕!顺势而为还是走弯路,就看一件事...
- 实验3-3 比较大小 (10 分)
- 190301每日一句
- mysql8忘记密码后重置密码
- (5)Spring框架----Bean作用域
- 记一次公众号开发途中的诡异事件
- directadmin php-fpm,Directadmin + Nginx + php fpm + location:找不到文件
- Django - Celery使用及介绍
- 解决pcntl_fork() has been disabled for security reasons in file /www/wwwroot/192.168.21.2/vendor/wor
- C语言实现3个数的最小公倍数和最大公约数
- 常见的数据埋点方式介绍
- 重庆卫生副高考试成绩查询2021,2021年重庆卫生资格成绩查询时间及查分入口【6月4日起】...
- 模仿猫眼电影静态网页展示
- 为什么post请求前先发一个options 请求?
- 电商巨头的「隐秘」战场