Springboot使用application.properties默认了很多配置。但需要自己添加一些配置的时候,我们应该怎么做呢。

如果继续在application.properties中添加

如:

test.t1=t1
test.t2=t2

定义配置类:

package me.shijunjie.config.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="test")
public class Test1Settings {
private String t1;
private String t2;
public String getT1() {
return t1;
}
public void setT1(String t1) {
this.t1 = t1;
}
public String getT2() {
return t2;
}
public void setT2(String t2) {
this.t2 = t2;
}
@Override
public String toString() {
return "Test1Settings [t1=" + t1 + ", t2=" + t2 + "]";
}
}

若用新的配置文件

如我新建一个test.properties

test.t1=aaaa
test.t2=bbbb

需定义如下配置类

package me.shijunjie.config.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="test", locations="classpath:test.properties")
public class Test2Settings {
private String t1;
private String t2;
public String getT1() {
return t1;
}
public void setT1(String t1) {
this.t1 = t1;
}
public String getT2() {
return t2;
}
public void setT2(String t2) {
this.t2 = t2;
}
@Override
public String toString() {
return "Test2Settings [t1=" + t1 + ", t2=" + t2 + "]";
} }

最后注意在spring Boot入口类加上@EnableConfigurationProperties

package me.shijunjie.testspringboot3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan; import me.shijunjie.config.properties.Test1Settings;
import me.shijunjie.config.properties.Test2Settings; @SpringBootApplication
@EnableConfigurationProperties({Test1Settings.class, Test2Settings.class})
@ComponentScan("me.shijunjie")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}

使用定义的properties(不知道为什么,我这边用了Test1Settings就不能用Test2Settings,如果想两个都用,可以将Setting作为component扫描进spring容器中)

package me.shijunjie.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import me.shijunjie.config.properties.Test1Settings;
import me.shijunjie.config.properties.Test2Settings; @RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private Test1Settings test1Settings;
@Autowired
private Test2Settings test2Settings; @RequestMapping("/testProperties")
public String testProperties() {
System.out.println(test1Settings);
System.out.println(test2Settings);
return "ok";
}
}

[十七]SpringBoot 之 使用自定义的properties的更多相关文章

  1. springboot中使用自定义的properties属性

    在application.properties中添加属性ai.name=明ai.age=22ai.sex=男定义配置类如下,前缀(prefix)可自定义修改,本文为 ai.@Configuration ...

  2. Springboot中读取自定义名称properties的

    Springboot读取自定义的配置文件时候,使用@value,一定要指定配置文件的位置!  否则报错参数化异常!

  3. 170705、springboot编程之自定义properties

    spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,可以这样用,如下! 在application.properties文件中增加信息 1 ...

  4. 【spring boot】SpringBoot初学(2) - properties配置和读取

    前言 只是简单的properties配置学习,修改部分"约定"改为自定义"配置".真正使用和遇到问题是在细看. 一.主要 核心只是demo中的: @Proper ...

  5. SpringBoot系列之自定义starter实践教程

    SpringBoot系列之自定义starter实践教程 Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...

  6. SpringBoot(二) SpringBoot核心配置文件application.yml/properties

    我们都知道在Spring中有着application.xml文件对Spring进行相关配置,通过web.xml中的contextConfigLocation指定application.xml文件所在位 ...

  7. SpringBoot入门教程(三)通过properties实现多个数据库环境自动切换配置

    前面的文章已经介绍了CentOS部署SpringBoot项目从0到1的详细过程,包括Linux安装ftp.Tomcat以及Java jdk的全部过程.这篇文章主要介绍关于springboot如何通过多 ...

  8. SpringBoot Mybatis EnumTypeHandler自定义统一处理器

    需求 mybatis目前已经内嵌入了springboot中了,这说明其目前在数据访问层的绝对优势.而我们在开发的过程中,往往会在程序中使用枚举(enum) 来表示一些状态或选项,而在数据库中使用数字来 ...

  9. SpringBoot:四种读取properties文件的方式

    前言 在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作.今天就分享四种在Springboot中获取配置文件的方式. 注:前三种测试配置文件为springboot默认的applic ...

随机推荐

  1. BZOJ2140_稳定婚姻_KEY

    题目传送门 暴力直接对于每个点跑一遍二分图匹配,能拿四十分. 然而我们考虑正解. 对于一对Couple我们建♂->♀的一条边,对于一对曾经有恋情的情侣我们建♀->♂的一条边. 跑Tarja ...

  2. [agc002D]Stamp Rally-[并查集+整体二分]

    Description 题目大意:给你一个n个点m个条边构成的简单无向连通图,有Q组询问,每次询问从两个点x,y走出两条路径,使这两条路径覆盖z个点,求得一种方案使得路径上经过的边的最大编号最小.n, ...

  3. 使GDAL库支持中文路径或中文文件名的处理方法

    之前生成的gdal 2.1.1动态库,在通过命令行执行时,遇到有中文路径或中文图像名时,GDALOpen函数不能正确的被调用,如下图: 解决方法: 1.      在所有使用GDALAllRegist ...

  4. 【LG3234】[HNOI2014]抄卡组

    题面 题解 分三种情况: 若所有串都没有通配符,直接哈希比较即可. 若所有串都有通配符, 把无通配符的前缀 和 无通配符的后缀哈希后比较即可. 中间部分由于通配符的存在,一定可以使所有串匹配. 若部分 ...

  5. 用Micro:bit做剪刀、石头、布游戏

    剪刀.石头.布游戏大家都玩过,今天我们用Micro:bit建一个剪刀.石头.布游戏! 第一步,起始 当你摇动它时,我们希望the micro:bit选择剪刀.石头.布.尝试创建一个on shake b ...

  6. mybatis拦截器使用

    目录 mybatis 拦截器接口Interceptor spring boot + mybatis整合 创建自己的拦截器MyInterceptor @Intercepts注解 mybatis拦截器入门 ...

  7. 【实用】巧用For xml 生成HTML代码

    可以利用SQL的For xml直接生成HTML结构,比如我想生成如下结构: <li> <img src="..."/> <input type=&qu ...

  8. Spark SQL、DataFrame和Dataset——转载

    转载自:  Spark SQL.DataFrame和Datase

  9. Centos上搭建git服务

    1.安装Git $ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel $ yum ...

  10. Scrum立会报告+燃尽图(十一月十六日总第二十四次):功能开发与设计页面

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2384 项目地址:https://git.coding.net/zhang ...