示例如下:

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 自动配置自定义配置文件的更多相关文章

  1. Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件

    本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...

  2. Springboot 系列(三)Spring Boot 自动配置原理

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...

  3. Spring Boot自动配置原理与实践(一)

    前言 Spring Boot众所周知是为了简化Spring的配置,省去XML的复杂化配置(虽然Spring官方推荐也使用Java配置)采用Java+Annotation方式配置.如下几个问题是我刚开始 ...

  4. Spring Boot自动配置如何工作

    通过使用Mongo和MySQL DB实现的示例,深入了解Spring Boot的@Conditional注释世界. 在我以前的文章“为什么选择Spring Boot?”中,我们讨论了如何创建Sprin ...

  5. Spring Boot 自动配置之@Conditional的使用

    Spring Boot自动配置的"魔法"是如何实现的? 转自-https://sylvanassun.github.io/2018/01/08/2018-01-08-spring_ ...

  6. Spring Boot自动配置原理与实践(二)

    前言 在之前的博文(Spring Boot自动配置原理与实践(一))中,已经介绍了Spring boot的自动配置的相关原理与概念,本篇主要是对自动配置的实践,即自定义Starter,对原理与概念加深 ...

  7. Spring Boot自动配置实战

    上篇讲述了Spring Boot自动配置的原理,本篇内容就是关于该核心原理的实际应用.需求即当某个类存在的时候,自动配置这个类的bean并且这个bean的属性可以通过application.prope ...

  8. Spring Boot自动配置与Spring 条件化配置

    SpringBoot自动配置 SpringBoot的自动配置是一个运行时(应用程序启动时)的过程,简化开发时间,无需浪费时间讨论具体的Spring配置,只需考虑如何利用SpringBoot的自动配置即 ...

  9. Spring Boot自动配置原理、实战

    Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...

随机推荐

  1. java——重载解析、静态绑定、动态绑定

    重载解析: a被声明为A类型的对象,调用method()方法时,如果有多个同名方法,参数不同,编译器将列举所用类A的method()方法和所用父类中public类型的method()方法,编译器查看这 ...

  2. c++ 封装线程库 2

    1.2线程回收: 首先得知道线程的两个状态: Joinable Detached 简单理解,如果一个线程是joinable的状态,那么这样的线程,就必须使用pthread_join来回收,否则程序结束 ...

  3. Lights Out Game

    Lights Out Game 在线的游戏:http://www.neok12.com/games/lights-out/lights-out.htm 瞎试一阵子未成之后,终于找到了标准答案:http ...

  4. MQTT学习之一

    一MQTT特性: 基于C/S,发布订阅(发布者服务器->云平台代理->订阅客户端)一对多结构,适用于低带宽高延时,基于TCP/IP之上.

  5. CAD安装失败怎样卸载CAD 2018?错误提示某些产品无法安装

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  6. [转]FireFox与IE 下js兼容触发click事件的代码

    本文转自:http://www.jb51.net/article/16549.htm FireFox与IE 下js兼容触发click事件 ,对于需要兼容这两者的朋友,就需要参考下下面的代码了<a ...

  7. (转)使用HMC接管通过串口或显卡安装的分区操作系统

    使用HMC接管通过串口或显卡安装的分区操作系统 原文:http://m.blog.itpub.net/23135684/viewspace-1062084/ 这是一个真实的案例,客户有一台P550的服 ...

  8. 自己的spring boot starter

    这篇文章说的更加详细具体:https://www.cnblogs.com/hjwublog/p/10332042.html 在刚开始看spring boot的时候,发现这么多starter,不免觉得好 ...

  9. pat1093. Count PAT's (25)

    1093. Count PAT's (25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng The strin ...

  10. C面向对象编程

    C语言面向对象编程 1. 定义一个SuperObject结构体, 里面包含最少的元素, 但是确实每一个对象都含有的, 这样可以实现多态 2. 每一个对象都是基于类的, 我们知道类都是单例对象, 所以我 ...