玩转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配置的实践,想象这样一个场景:你的系统 ...
随机推荐
- Android之RelativeLayout相对布局
1.相关术语解释 1.基本属性 gravity :设置容器内组件的对齐方式 ignoreGravity : 设置该属性为true的组件,将不受gravity属性的影响 2.根据父容器定位 layout ...
- java实现从实体到SQL语句的转换
使用过Hibernate,EF之类的ORM框架都知道一般的CRUD之类的简单操作,只要调用框架封装好了的方法,框架就自动生成相应的SQL语句了,参照实习公司给的代码,那个是C#版的,今天弄了一下jav ...
- visio去除直线交叉处的歪曲
1 问题描述 Visio画图时,两根直线交叉时,总是默认会出现一个跨线的标志,如下图所示: 2 解决办法 在2007前的版本,可以通过以下方式解决: 选中线条,然后菜单的格式->行为->连 ...
- 如何解决mysql服务器load高
.登录主机 # ssh hostname .确定是否是mysql导致 # top .查看是哪些sql正在慢查询 # mysql -h hostname -P port -u username # sh ...
- anime.js 学习笔记
官网演示/文档 anime.js 是一个简便的JS动画库,用法简单而且适用范围广,涵盖CSS,DOM,SVG还有JS的对象,各种带数值属性的东西都可以动起来. 实际演示和代码,官网写得很详细清楚了,这 ...
- Spring注解驱动开发(四)-----aop、声明式事务
AOP 概念 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式:-----基于动态代理 一个aop示例 1.导入aop模块:Spring AOP:(spring-aspects ...
- 组件:参数验证props:组件参数验证语法
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- Vue 获取dom元素之 ref 和 $refs 详解
一.$refs 一个对象,持有ref注册过的所有元素或子组件.(注册过的 ref 的集合) 二.ref 被用来给元素或子组件注册引用信息.若用在dom元素上,引用指向的就是dom元素:若用在子组件上, ...
- 加载框(loading)
一般在用户提交数据或者新加载页面时,请求服务器的过程,页面没有响应,但是用户并不知道,此时在发生什么.这时,就需要loading框给用户提示,增加用户体验. 1.引入loading.css. html ...
- Cesium官方教程5--地形图层
原文地址:https://cesiumjs.org/tutorials/Terrain-Tutorial/ Cesium支持渐进流式加载和渲染全球高精度地形,并且包含海.湖.河等水面效果.相对2D地图 ...