项目中经常需要进行一些配置,一般会使用springboot默认的application.properties文件,也可以自己创建配置文件

一,application.properties配置

logging.path=/Users/zhang_guang_yang/IDEA15/Logs/
logging.file=SpringBoot.log # strings
com.gy.dateFormatter=dd-MM-yyyy # strings
com.pleasure.local=/files # array
com.pleasure.urls[0]=http://www.baidu.com/
com.pleasure.urls[1]=http://www.google.com/
com.pleasure.urls[2]=http://www.blog.csdn.com/ # map
com.pleasure.admin[username]=pleasure
com.pleasure.admin[password]=123456

  读取

  1,通过一个配置类来读取:

package com.example.demo.config;

import org.springframework.boot.autoconfigure.session.StoreType;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by zhang_guang_yang on 2018/12/2.
*/ @Component
@ConfigurationProperties(prefix = "com.pleasure")
public class AppProperties { private String local;
private Map<String, String> admin = new HashMap<String ,String>();
private List<String> urls = new ArrayList<String >(); public String getLocal() {
return local;
} public void setLocal(String local) {
this.local = local;
} public Map<String, String> getAdmin() {
return admin;
} public void setAdmin(Map<String, String> admin) {
this.admin = admin;
} public List<String> getUrls() {
return urls;
} public void setUrls(List<String> urls) {
this.urls = urls;
}
}

  获取属性

@RestController
@EnableAutoConfiguration
public class PropertiesController { @Autowired
private AppProperties appProperties; @RequestMapping("/properties")
public String properties(){ System.out.println("local: " + appProperties.getLocal());
System.out.println("admin: " + appProperties.getAdmin().get("username");
return success;
}
}

  2, @Value读取

  @Value("${com.gy.dateFormatter}")
private String dateFormat;

  3,通过Environment对象读取

  

   @Autowired
private Environment evn;

  整个实现

  

package com.example.demo.controllers;

import com.example.demo.config.AppProperties;
import com.example.demo.domain.Info;
import com.sun.tools.javac.comp.Env;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by zhang_guang_yang on 2018/11/20.
*/
@RestController
@EnableAutoConfiguration
public class PropertiesController { @Autowired
private AppProperties appProperties; @Value("${com.gy.dateFormatter}")
private String dateFormat; @Autowired
private Environment evn; @Autowired
private Info info; @RequestMapping("/properties")
public String properties(){ System.out.println("local: " + appProperties.getLocal());
System.out.println("admin: " + appProperties.getAdmin().get("username") + " " + appProperties.getAdmin().get("password"));
System.out.println("url: " + appProperties.getUrls().get(0));
System.out.println("date formatter: " + dateFormat); System.out.println(evn.getProperty("com.gy.dateFormatter")); System.out.println("info: " + info.getName());
return "success";
}
}

  

二,自定义properties文件和类

  1,info.properties

zgy.info.version=1.0
zgy.info.name="good mall"
zgy.info.bundleId="zgy.good.mall"

  2, 定义类

package com.example.demo.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource; /**
* Created by zhang_guang_yang on 2018/11/20.
*/ @ConfigurationProperties(prefix = "zgy.info")
@PropertySource("classpath:config/info.properties") public class Info { private String version;
private String name;
private String bundleId; public String getVersion() {
return version;
} public void setVersion(String version) {
this.version = version;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getBundleId() {
return bundleId;
} public void setBundleId(String bundleId) {
this.bundleId = bundleId;
} }

  3,注册扫描

@ComponentScan(basePackages = {"com.example.demo.domain"})
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

  4,获取属性

  

  @Autowired
private Info info;

  

SpringBoot-(5)-properties的使用的更多相关文章

  1. SpringBoot application.properties (application.yml)优先级从高到低

    SpringBoot application.properties(application.yml) 优先级从高到低 SpringBoot配置文件优先级从高到低 =================== ...

  2. springboot application.properties配置大全

    springboot application.properties配置大全 官方文档 https://docs.spring.io/spring-boot/docs/current/reference ...

  3. springboot~application.properties和application.yml的使用

    在springboot框架里进行项目开始时,我们在resource文件夹里可以存放配置文件,而格式可以有两种,properties和yml,前者是扁平的k/v格式,而后者是yml的树型结构,我们建议使 ...

  4. SpringBoot标准Properties

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  5. springboot application.properties 常用完整版配置信息

    从springboot官方文档中扒出来的,留存一下以后应该会用到 # ================================================================= ...

  6. springboot获取properties文件的配置内容(转载)

    1.使用@Value注解读取读取properties配置文件时,默认读取的是application.properties. application.properties: demo.name=Name ...

  7. springboot读取properties和yml配置文件

    一.新建maven工程:springboot-configfile-demo,完整工程如下: pom.xml <?xml version="1.0" encoding=&qu ...

  8. SpringBoot yml properties文件

    一.在SpringBoot实现属性注入: 1).添加pom依赖jar包: 1 <!-- 支持 @ConfigurationProperties 注解 --> 2 <!-- https ...

  9. springBoot application.properties 基础配置

    # 文件编码 banner.charset= UTF-8 # 文件位置 banner.location= classpath:banner.txt # 日志配置 # 日志配置文件的位置. 例如对于Lo ...

  10. SpringBoot application.properties 配置项详解

    参考: http://blog.csdn.net/lpfsuperman/article/details/78287265### # spring boot application.propertie ...

随机推荐

  1. js-控制浏览器和移动端的后退按钮 . popstate

    //控制浏览器和移动端的后退按钮 if (window.history && window.history.pushState) { $(window).on('popstate', ...

  2. jquery 焦点轮播图控制每张图片停留不同时间

    轮播代码是代签博客园一位前辈写的代码,这里作了点小修改,实现了每张图片停留不同时间 *{ padding:0px; border:0px; margin:0px; } ul { list-style: ...

  3. Codeforces Gym101606 J.Just A Minim (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))

    J Just A Minim 超级无敌大水题,但是被精度卡了一手,输出要精确到小数点后6位,我直接输出的... 代码: 1 #include<iostream> 2 #include< ...

  4. LibieOJ 6170 字母树 (Trie)

    题目链接 字母树 (以每个点为根遍历,插入到trie中,统计答案即可)——SamZhang #include <bits/stdc++.h> using namespace std; #d ...

  5. chattr&chown&cat&cut&useradd&passwd&chage&usermod

    1.用chattr命令防止系统中某个关键文件被修改 chattr +i /etc/resolv.conf chattr -i /etc/resolv.conf 要想修改此文件就要把i属性去掉 lsat ...

  6. CodeChef - RIN Course Selection

    Read problems statements in Mandarin Chineseand Russian. Rin is attending a university. She has M se ...

  7. Java中判断String对象是否为空的方法

    Java原生的方法: String对象中有一个isEmpty的方法判断是否为空,其实isEmpty完全等同于string.length()==0,注意如果String本身是null,那么使用strin ...

  8. Cesium之3D拉伸显示行政区

    转自原文 Cesium之3D拉伸显示行政区含GeoJSON数据生成过程GDAL的ogr2ogr Cesiumjs 是一套javascript库,用来渲染3D地球,2D区域地图,和多种GIS要素.不需要 ...

  9. 百度 BAE 项目部署

    转载:http://www.cnblogs.com/shamoyuu/p/node_bae.html 百度有一个应用引擎,价格非常便宜,Java的tomcat每天4毛钱,node每天2毛钱,我以前在上 ...

  10. Android开发——进程间通信之AIDL(二)

    0.  前言 不论是Android还是其它操作系统.都会有自己的IPC机制.所谓IPC(Inter-Process Communication)即进程间通信.首先线程和进程是非常不同的概念,线程是CP ...