Spring/Spring Boot的外部化配置

不论是一个简单的Java程序或者是基于Spring或者Spring Boot框架实现的程序,都存在外部化配置信息的需求,例如一个抽奖程序需要制定随机器的种子值,或者与数据库建立连接的url/username/password,这些配置信息你都不希望直接固定写入程序中,因此需要一种方式能够外部化制定这些配置信息,在代码的对这些数据的使用点读取对应的配置来实现动态化程序的行为。

1.  环境变量(System.getEnv)

Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.
To get a specific environment variable you can use System.getenv(String name).

2. Java系统属性(System.getProperties)

System properties are set on the Java command line using the -Dpropertyname=value syntax. They can also be added at runtime using System.setProperty(String key, String value) or via the various System.getProperties().load() methods.
To get a specific system property you can use System.getProperty(String key) or System.getProperty(String key, String def). As it happens, Java chose to expose OS variables as properties (just as Java exposes current directory and "other stuff" as properties)

Different from Environment Variables: https://www.baeldung.com/java-system-get-property-vs-system-getenv

3. spring的外部化配置策略

spring.config.location, 只对spring boot有效,查找配置文件的详细地址。

Property and Enviroment

application.properties/.yml

Common sub project configuration

above spring 3.1 and below spring 3.1 ,  propertysourceplaceholder propertyplaceholder   <context: place-holder> @PropertySource

原文地址:https://www.cnblogs.com/dongyuanshi/p/9869118.html

时间: 2024-08-30 05:40:08

Spring/Spring Boot的外部化配置的相关文章

Spring Boot 外部化配置(二) - @ConfigurationProperties 、@EnableConfigurationProperties

目录 3.外部化配置的核心 3.2 @ConfigurationProperties 3.2.1 注册 Properties 配置类 3.2.2 绑定配置属性 3.1.3 ConfigurationPropertiesAutoConfiguration 4.总结 3.外部化配置的核心 ????????接着上一章,<Spring Boot 外部化配置(一)> 3.2 @ConfigurationProperties 众所周知,当 Spring Boot 集成外部组件后,就可在 propertie

Spring配置文件外部化配置及.properties的通用方法

摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java 解决问题:如何正确使用.properties配置文件. 若是有其他代码需要此Spring属性配置,将Spring配置中的属性值设置迁移到外部的属性文件中,是必需的操作,这也可以使Spring配置文件更易读.在这里我们不仅要讨论Spring的外部化配置,还要深入探讨配置化文件(即.propertie

spring外部化配置

例如 1 <bean id="dataSource" 2 3 class="....." 4 5 p:username="aa" 6 7 p:password="11" 8 9 > 以外部文件配置化的方式,配置属性. 在db.properties jdbc.username=aa jdbc.password=22 所以以上的bean可以写成 1 <bean id="dataSource"

二、外部化配置--SpringBoot功能

一.外部化配置 Spring Boot将你的配置外部化,因此你可以在不同的环境下运行相同的代码.你可以使用properties文件,YAML文件,环境变量,命令行参数在外部配置.使用@Value注解可以直接将属性值注入到bean中,通过Spring的Environment抽象访问,或通过@ConfigurationProperties绑定到结构化对象. Spring Boot有多种外部配置方式,优先级如下: 当devtools开启时,$HOME/.config/spring-boot下devto

Spring Boot Tomcat 容器化部署实践与总结

在平时的工作和学习中经常会构建简单的web应用程序.如果只是HelloWorld级别的程序,使用传统的Spring+SpringMVC框架搭建得话会将大部分的时间花费在搭建框架本身上面,比如引入SpringMVC,配置DispatcheherServlet等.并且这些配置文件都差不多,重复这些劳动似乎意义不大.所以使用Springboot框架来搭建简单的应用程序显得十分的便捷和高效. 前两天在工作中需要一个用于测试文件下载的简单web程序,条件是使用Tomcat Docker Image作为载体

Spring Boot基础5-日志配置-logback和log4j2

源码地址:https://github.com/roncoo/spring-boot-demo 支持日志框架:Java Util Logging, Log4J2 and Logback,默认是使用logback 配置方式:默认配置文件配置和引用外部配置文件配置 一. 默认配置文件配置(不建议使用:不够灵活,对log4j2等不够友好) #日志文件名,比如:roncoo.log,或者是 /var/log/roncoo.log logging.file=roncoo.log # 日志级别配置,比如:

Spring Boot 探索系列 - 自动化配置篇

26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging for all internal logging, but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging,Log4J, Log4J2 an

Spring Boot教程30——Tomcat配置

本节的配置方法对Tomcat.Jetty和Undertow等内嵌servlet容器都是通用的. 1.Properties配置Tomcat 关于Tomcat的所有属性都在org.springframework.boot.autoconfigure.web.ServerProperties配置类中做了定义,我们只需在application.properties配置属性做配置即可.通用的Servlet容器配置都以“server”作为前缀,而Tomcat特有配置都以“server.tomcat”作为前缀

学记:为spring boot写一个自动配置

spring boot遵循"约定由于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇不是借助代码的生成来实现的,而是通过条件注解来实现的. 自动配置AutoConfiguration是实现spring boot的重要原理,理解AutoConfiguration的运行原理特别重要,自己写一个AutoConfiguration可以加深我们对spring boot的理解. 1.定义Type-saf