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 ...
随机推荐
- vue 父组件调用子组件的函数
子组件 <template> <div> child </div> </template> <script> export default ...
- hadoop - hbase 单机版的安装部署
1. 下载安装包. http://www.apache.org/dyn/closer.lua/hbase/ 选择版本,单机进入以下页面 2. tar xfz hbase-1.2.6-bin.tar.g ...
- 转 RMAN-20005: target database name is ambiguous
发生的这个错误的由于: 在RMAN CATALOG中,register了一个name叫test的数据库,后来这个库被我搞坏了.就重建了一个test的数据库,名称没有更改,又重新register到RMA ...
- json处理第二篇:利用fastjson处理json
fastjson是阿里开源的工具包,主要是利用com.alibaba.fastjson.JSON及其两个子类com.alibaba.fastjson.JSONObject.com.alibaba.fa ...
- TestNG的testng.xml配置概述
TestNG提供的annotaions用来辅助定义测试类. TestNG的testng.xml配置文件用来辅助定义执行什么样的测试,即testng.xml更像是一个测试规划. testng.xml配置 ...
- Android中的时间格式的校验
public class MainActivity extends Activity implements OnClickListener{ private Button btn1; private ...
- SpringBoot如何集成Jedis
添加jedis依赖 在项目pom.xml文件中添加依赖 <!-- 添加jedis依赖 --> <dependency> <groupId>redis.clients ...
- Akka探索第一个例子by fsharp 1
如何构建一套分布式程序一直是我想知道的问题. Akka就是一套用来开发分布式系统的开发库.当然开发分布式系统只是它的能力之一.除此之外高度抽象的并行运算能力,轻量级的消息系统,容错能力都是该库的特点. ...
- DO、PO、BO、DTO、VO等概念
PO 全称为:Persistant Object,持久化对象,与数据库结构映射的实体,数据库中的一条数据即为一个 PO 对象. BO 全称为:Business Object,业务对象,主要作用是把业务 ...
- 一些实用的浏览器meta
标签: 兼容性 meta 通用 <!--声明文档使用的字符编码--> <meta charset='utf-8′> <!--viewport定义--> <me ...