当前位置: 首页 > news >正文

SpringBoot中@Import和@ImportResource和@PropertySource

1. @Import

@Import注解是引入java类:

  • 导入@Configuration注解的配置类(4.2版本之前只可以导入配置类,4.2版本之后也可以导入普通类)
  • 导入ImportSelector的实现类
  • 导入ImportBeanDefinitionRegistrar的实现类
@SpringBootApplication
@Import(MyCustomize.class)
public class StartBootReadConfig {public static void main(String[] args) {SpringApplication.run(StartBootReadConfig.class, args);}@AutowiredMyCustomize myCustomize;@PostConstructpublic void init(){myCustomize.printf();}
}

 

2. @ImportResource


@ImportResource是引入spring配置文件.xml,它导入的是使用xml配置文件注入的对象。

新建测试类:

public class MyCustomize {public void printf(){System.out.println("MyCustomize.printf");}
}

新建一个bean的配置文件mycustomize.xml:

<?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="mycustomize" name="mycustomize" class="com.lx.readconfig.service.MyCustomize"></bean>
</beans>

使用ImportResource导入:

@SpringBootApplication
@ImportResource("mycustomize.xml")
public class StartBootReadConfig {public static void main(String[] args) {SpringApplication.run(StartBootReadConfig.class, args);}@AutowiredMyCustomize myCustomize;@PostConstructpublic void init(){myCustomize.printf();}}

 

 

3.@PropertySource

@PropertySource是引入自定义配置文件(yml或properties),用于spring boot 配置文件(yml或properties)与实体属性映射。

@Component
//@PropertySource("classpath:myext.properties")
//PropertySource默认不支持yml或者yaml的解析
@PropertySource(value = "classpath:myext.yml", factory = CustomizePropertySourceFactory.class)
@ConfigurationProperties(prefix = "keyfieldpropertiesext")
@Data

http://www.lryc.cn/news/497114.html

相关文章:

  • OpenCV 简介与安装方法
  • pycharm基本库安装的几种方法
  • 安装更新upgrade导致ubuntu崩溃
  • 数学建模选MATLAB还是Python?
  • python数组增加元素
  • 【笔记】离散数学 1-3 章
  • AI技术在电商行业中的应用与发展
  • 【Linux篇】权限管理 - 用户与组权限详解
  • 【AI系统】昇腾推理引擎 MindIE
  • 美畅物联丨智能监控,高效运维:视频汇聚平台在储能领域的实践探索
  • [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED]
  • 12.2深度学习_视觉处理CNN_池化层、卷积知识
  • Kafka 常见面试题深度解析
  • LVS默认的工作模式支持哪些负载均衡算法?
  • 洛谷P2670扫雷游戏(Java)
  • 【算法】【优选算法】位运算(下)
  • 前端性能优化篇:防抖和节流
  • 同为科技(TOWE)柔性定制化PDU插座
  • 【云原生系列】云计算中的负载均衡是什么,有什么用
  • 工业—使用Flink处理Kafka中的数据_ChangeRecord2
  • 【Java-数据结构篇】Java 中栈和队列:构建程序逻辑的关键数据结构基石
  • 工业—使用Flink处理Kafka中的数据_ProduceRecord1
  • 探索CSS版心布局:构建现代网页的黄金比例
  • 华为NPU服务器昇腾Ascend 910B2部署通义千问Qwen2.5——基于mindie镜像一路试错版(三)
  • 详解Java数据库编程之JDBC
  • 基于MFC实现的人机对战五子棋游戏
  • AIGC 时代的文学:变革与坚守
  • InfluxDB 集成 Grafana
  • 笔记本电脑usb接口没反应怎么办?原因及解决方法
  • 【开源】A060-基于Spring Boot的游戏交易系统的设计与实现