Spring boot 自动配置自定义配置文件
示例如下:
1. 新建 Maven 项目 properties
2. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.java</groupId>
<artifactId>properties</artifactId>
<version>1.0.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent> <dependencies> <!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency> <!-- 热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> <build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3. 配置文件 user-defined.properties
aaa.url.login=aaa-login.html
aaa.url.order=aaa-order.html bbb.goods.price=500
bbb.goods.weight=1000
4. PropertiesStarter.java
package com.java; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* 主启动类
*
* @author Storm
*
*/
@SpringBootApplication
public class PropertiesStarter { public static void main(String[] args) {
SpringApplication.run(PropertiesStarter.class, args);
} }
5. URLProperties.java
package com.java.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import lombok.Data; /**
* 前缀为aaa.url的配置自动注入到对应属性中
*
* @author Storm
*
*/
@Data
@ConfigurationProperties(prefix = "aaa.url")
public class URLProperties { private String login;
private String order; }
6. GoodsProperties.java
package com.java.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import lombok.Data; /**
* 前缀为bbb.goods的配置自动注入到对应属性中
*
* @author Storm
*
*/
@Data
@ConfigurationProperties(prefix = "bbb.goods")
public class GoodsProperties { private String price;
private String weight; }
7. PropertiesConfig.java
package com.java.config; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import com.java.properties.GoodsProperties;
import com.java.properties.URLProperties; /**
* 用户自定义配置文件配置类
*
* @author Storm
*
*/
@Configuration
@PropertySource({ "classpath:user-defined.properties" })
@EnableConfigurationProperties({ URLProperties.class, GoodsProperties.class })
public class PropertiesConfig { }
8. DemoController.java
package com.java.controller; import java.util.HashMap;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.java.properties.GoodsProperties;
import com.java.properties.URLProperties; @RestController
public class DemoController { @Autowired
private URLProperties url; @Autowired
private GoodsProperties goods; @GetMapping("/getProperties")
public Map<String, Object> getProperties() { System.out.println(url.getOrder()); Map<String, Object> map = new HashMap<>();
map.put("url", url);
map.put("goods", goods);
return map;
} }
9. 运行 PropertiesStarter.java ,启动测试
浏览器输入 http://localhost:8080/getProperties
返回结果如下:
{"goods":{"price":"500","weight":"1000"},"url":{"login":"aaa-login.html","order":"aaa-order.html"}}
配置加载成功!
Spring boot 自动配置自定义配置文件
.
Spring boot 自动配置自定义配置文件的更多相关文章
- Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件
本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...
- Springboot 系列(三)Spring Boot 自动配置原理
注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...
- Spring Boot自动配置原理与实践(一)
前言 Spring Boot众所周知是为了简化Spring的配置,省去XML的复杂化配置(虽然Spring官方推荐也使用Java配置)采用Java+Annotation方式配置.如下几个问题是我刚开始 ...
- Spring Boot自动配置如何工作
通过使用Mongo和MySQL DB实现的示例,深入了解Spring Boot的@Conditional注释世界. 在我以前的文章“为什么选择Spring Boot?”中,我们讨论了如何创建Sprin ...
- Spring Boot 自动配置之@Conditional的使用
Spring Boot自动配置的"魔法"是如何实现的? 转自-https://sylvanassun.github.io/2018/01/08/2018-01-08-spring_ ...
- Spring Boot自动配置原理与实践(二)
前言 在之前的博文(Spring Boot自动配置原理与实践(一))中,已经介绍了Spring boot的自动配置的相关原理与概念,本篇主要是对自动配置的实践,即自定义Starter,对原理与概念加深 ...
- Spring Boot自动配置实战
上篇讲述了Spring Boot自动配置的原理,本篇内容就是关于该核心原理的实际应用.需求即当某个类存在的时候,自动配置这个类的bean并且这个bean的属性可以通过application.prope ...
- Spring Boot自动配置与Spring 条件化配置
SpringBoot自动配置 SpringBoot的自动配置是一个运行时(应用程序启动时)的过程,简化开发时间,无需浪费时间讨论具体的Spring配置,只需考虑如何利用SpringBoot的自动配置即 ...
- Spring Boot自动配置原理、实战
Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...
随机推荐
- js input复选框选中父级同时子级也选中
js实现复选框选中父级元素子级元素也选中,没有子级元素选中父级也不选中的效果 HTML <tr> <td> <label> <input name=" ...
- Git命令行中文显示错误
中文文件名乱码(git status.git log.git pull .git push) #不对0x80以上的字符进行quote,解决git status/commit时中文文件名乱码git co ...
- es6.3学习笔记
es版本发布相当快,从1.x到2.x,再直接到5.x,6.x 索引这个词在es中有多重意思: 索引(名词):一个索引类似于传统数据库中的一个索引,用于存储关系型文档.索引的复数为indexes或ind ...
- ubuntu14.04.2安装 YouCompleteme
1 安装git ,按照这篇文章安装 http://www.cnblogs.com/or2-/p/4350252.html 2 安装编译需要的各种包 sudo apt-get install build ...
- idea编译golang插件总结
由于使用习惯了Idea 和vim插件.于是下载了idea的go插件并安装,可惜不支持go1.4 ,官方的go插件版本太低 133.326 — 133.9999 .只能手动编译 按照这个教程就可以 ht ...
- VB.NET中的模块
在C#中有“静态类”的概念,自然里边全部的方法都是静态的.这意味着你可以直接通过"类名.方法名"去调用(例如System的Math类就是典型).在VB.NET中,没有“静态类”的概 ...
- linux_api之文件操作
本篇索引: 1.引言 2.文件描述符 3.open函数 4.close函数 5.read函数 6.write函数 7.lseek函数 8.i/o效率问题 9.内核用以维护打开文件的相关数据结构 10. ...
- Machine learning preface
Machine learning Preface Definition T: Task E: Experience P: Performance Sequence: T -> E -> P ...
- python 钩子函数
python 在windows下监听键盘按键 使用到的库 ctypes(通过ctypes来调用Win32API, 主要就是调用钩子函数) 使用的Win32API SetWindowsHookEx(), ...
- 导出CSV
public FileResult ExportExcel() { var sbHtml = new StringBuilder(); sbHtml.Append("<table bo ...