玩转Spring Boot 自定义配置、导入XML配置与外部化配置
玩转Spring Boot 自定义配置、导入XML配置与外部化配置
Boot里面配置名称支持多种方式,例如:server.ssl.key-store,可以写成:server.ssl.keyStore都是可以的。下面具体详细介绍。
1.引用XML文件配置
2.引入多个@Configuration 配置类
3.引用自定义properties
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- file.upload.stor-path=E:/test/
file.upload.stor-path=E:/test/
(2)直接使用@Value注解方式,具体代码如下:
- package com.chengli.springboot.helloworld;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @SpringBootApplication
- public class SampleController {
- @Value(value = "${file.upload.stor-path}")
- private String storPath;
- @RequestMapping("/")
- String home() {
- return "Hello World! file.upload.stor-path为:" + storPath;
- }
- public static void main(String[] args) throws Exception {
- SpringApplication springApplication = new SpringApplication(SampleController.class);
- springApplication.run(args);
- }
- }
package com.chengli.springboot.helloworld; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@SpringBootApplication
public class SampleController {
@Value(value = "${file.upload.stor-path}")
private String storPath;@RequestMapping("/")
String home() {
return "Hello World! file.upload.stor-path为:" + storPath;
} public static void main(String[] args) throws Exception {
SpringApplication springApplication = new SpringApplication(SampleController.class);
springApplication.run(args);
}
}
第二种:
- package com.chengli.springboot.helloworld;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- @Component
- @ConfigurationProperties(prefix = "file.upload")
- public class FileUploadProperties {
- private String storPath;
- public String getStorPath() {
- return storPath;
- }
- public void setStorPath(String storPath) {
- this.storPath = storPath;
- }
- }
package com.chengli.springboot.helloworld; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "file.upload")
public class FileUploadProperties {
private String storPath;public String getStorPath() {
return storPath;
} public void setStorPath(String storPath) {
this.storPath = storPath;
}
}
(2)入口启动类代码如下:
- package com.chengli.springboot.helloworld;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @SpringBootApplication
- public class SampleController {
- @Autowired
- private FileUploadProperties fileUploadProperties;
- @RequestMapping("/")
- String home() {
- return "Hello World! file.upload.stor-path为:" + fileUploadProperties.getStorPath();
- }
- public static void main(String[] args) throws Exception {
- SpringApplication springApplication = new SpringApplication(SampleController.class);
- springApplication.run(args);
- }
- }
package com.chengli.springboot.helloworld; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@SpringBootApplication
public class SampleController {
@Autowired
private FileUploadProperties fileUploadProperties;@RequestMapping("/")
String home() {
return "Hello World! file.upload.stor-path为:" + fileUploadProperties.getStorPath();
} public static void main(String[] args) throws Exception {
SpringApplication springApplication = new SpringApplication(SampleController.class);
springApplication.run(args);
}
}
注意:这里我对FileUploadProperties使用了@Component注解,如果没有使用@Component注解,则需要在入口启动类上加上@EnableConfigurationProperties注解。Spring Boot 在properties文件中支持使用SpEL表达式,可以进行校验(校验注解使用的是javax.validation)等操作。
private String storPath;
4.外部化配置(配置方式与优先级)
5.application.properties文件按优先级,优先级高的会覆盖优先级低的
Spring Boot QQ交流群:599546061
玩转Spring Boot 自定义配置、导入XML配置与外部化配置的更多相关文章
- Dubbo 新编程模型之外部化配置
外部化配置(External Configuration) 在Dubbo 注解驱动例子中,无论是服务提供方,还是服务消费方,均需要转配相关配置Bean: @Bean public Applicatio ...
- SpringBoot外部化配置使用Plus版
本文如有任何纰漏.错误,请不吝指正! PS: 之前写过一篇关于SpringBoo中使用配置文件的一些姿势,不过嘛,有句话(我)说的好:曾见小桥流水,未睹观音坐莲!所以再写一篇增强版,以便记录. 序言 ...
- 关于SpringBoot的外部化配置使用记录
关于SpringBoot的外部化配置使用记录 声明: 若有任何纰漏.错误请不吝指出! 记录下使用SpringBoot配置时遇到的一些麻烦,虽然这种麻烦是因为知识匮乏导致的. 记录下避免一段时间后自己又 ...
- 玩转spring boot——properties配置
前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...
- Spring Boot 2.X(四):Spring Boot 自定义 Web MVC 配置
0.准备 Spring Boot 不仅提供了相当简单使用的自动配置功能,而且开放了非常自由灵活的配置类.Spring MVC 为我们提供了 WebMvcConfigurationSupport 类和一 ...
- 峰哥说技术:06-手撸Spring Boot自定义启动器,解密Spring Boot自动化配置原理
Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 06 峰哥说技术:手撸Spring Boot自定义启动器,解密Spring Boot自动化配置原理 Sp ...
- Spring Boot自定义配置与加载
Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...
- Spring Boot自定义Redis缓存配置,保存value格式JSON字符串
Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 ...
- Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践
Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践 本篇博文主要提供一个在 SpringBoot 中自定义 kafka配置的实践,想象这样一个场景:你的系统 ...
随机推荐
- 「题解」:07.18NOIP模拟赛T1:星际旅行
问题 A: 星际旅行 时间限制: 1 Sec 内存限制: 256 MB 题面 题面谢绝公开. 考试心路历程 拿到这道题感觉很懵逼,所以先搞的T2和T3,最后码了个暴力,结果还不如直接输出‘0’得分高 ...
- 「BZOJ2391」Cirno的忧郁
传送门 设p[0] = (-10001,-10001) 把所有点按p[0]极角排序, s[i][j]表示三角形p[0]p[i]p[j]内的总价值,若i到j极角增大则s为正,否则s为负. 那么答案就是按 ...
- 一个Js开发者学习Python的第一天
原文地址:小寒的博客 https://www.dodoblog.cn/blogs/5bf6b8fa0c09883d0f8aad13 作为一个有着足足两年半学习经验和一年半开发经验的js开发者,看着js ...
- 19-11-13-Night-∠
连夜补博客 ZJ: 看见T1就自闭了.(高考数学)(但是好像不是) 三个暴力就结束了. 35 Miemeng 20 00:00:41 10 00:00:41 10 00:00:41 40 00:00: ...
- springmvc 文件下载分批读取,防止内存溢出
参考 https://blog.csdn.net/u014732956/article/details/51404086
- 《DSP using MATLAB》Problem 8.32
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Windows API 23 篇 WTSQuerySessionInformation
函数原型:BOOLWINAPIWTSQuerySessionInformationW( IN ...
- Ubuntu时间管理方法
1. date 命令主要用于显示以及修改系统时间 2. hwclock 命令用于查看设置硬件时间,以及同步硬件时间与系统时间 # 显示硬件时间hwclock # 设置硬件时间hwclock -set ...
- mysql批量增加表中新列存储过程
一般访问量比较大的网站,请求日志表都是每天一张表独立创建. 业务需要为每张表都添加一个新列,纠结了半天,写了个存储过程如下: 日志表结构类型 tbl_ads_req_20140801, tbl_ads ...
- Gym-102141E
https://vjudge.net/problem/Gym-102141E 用set乱搞 #include<iostream> #include<cstdio> #inclu ...