SpringBoot-(5)-properties的使用
项目中经常需要进行一些配置,一般会使用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的使用的更多相关文章
- SpringBoot application.properties (application.yml)优先级从高到低
SpringBoot application.properties(application.yml) 优先级从高到低 SpringBoot配置文件优先级从高到低 =================== ...
- springboot application.properties配置大全
springboot application.properties配置大全 官方文档 https://docs.spring.io/spring-boot/docs/current/reference ...
- springboot~application.properties和application.yml的使用
在springboot框架里进行项目开始时,我们在resource文件夹里可以存放配置文件,而格式可以有两种,properties和yml,前者是扁平的k/v格式,而后者是yml的树型结构,我们建议使 ...
- SpringBoot标准Properties
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...
- springboot application.properties 常用完整版配置信息
从springboot官方文档中扒出来的,留存一下以后应该会用到 # ================================================================= ...
- springboot获取properties文件的配置内容(转载)
1.使用@Value注解读取读取properties配置文件时,默认读取的是application.properties. application.properties: demo.name=Name ...
- springboot读取properties和yml配置文件
一.新建maven工程:springboot-configfile-demo,完整工程如下: pom.xml <?xml version="1.0" encoding=&qu ...
- SpringBoot yml properties文件
一.在SpringBoot实现属性注入: 1).添加pom依赖jar包: 1 <!-- 支持 @ConfigurationProperties 注解 --> 2 <!-- https ...
- springBoot application.properties 基础配置
# 文件编码 banner.charset= UTF-8 # 文件位置 banner.location= classpath:banner.txt # 日志配置 # 日志配置文件的位置. 例如对于Lo ...
- SpringBoot application.properties 配置项详解
参考: http://blog.csdn.net/lpfsuperman/article/details/78287265### # spring boot application.propertie ...
随机推荐
- (1)angularJs
一. 1.下载 https://angularjs.org/ 2.网络引用 https://code.angularjs.org/ 3.模块内引用 angularjs <body ng-app& ...
- (6)C#事务处理
为了方便移到了ADO.NET分类里 事务的主要特征是,任务要么全部完成,要么都不完成 事务常用于写入或更新数据库中的数据.将数据写入文件或注册表也可以使用事物. ADO.NET不支持跨越多个连接的事物 ...
- 公司内部技术分享之Vue.js和前端工程化
今天主要的核心话题是Vue.js和前端工程化.我将结合我这两年多的工作学习经历来谈谈这个,主要侧重点是前端工程化,Vue.js侧重点相对前端工程化,比重不是特别大. Vue.js Vue.js和Rea ...
- ML| EM
What's xxx The EM algorithm is used to find the maximum likelihood parameters of a statistical model ...
- 记Weblogic采用RAC方式链接数据库遇到的问题
前几天,去客户现场部署系统,WEBLOGIC连接数据库使用RAC方式连接,好几个人弄了一下午愣是没搞定,总是报SID错误 开始一致认为是防火墙的原因,后来SSH登陆应用服务器后,再TELNET数据 ...
- 45个非常有用的Oracle查询语句(转自开源中国社区)
日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...
- SilverLight:布局(1) Border(边框)对象、Grid(网格)对象
ylbtech-SilverLight-Layout:布局(1) Border(边框)对象.Grid(网格)对象 A, Border(边框)对象 B, Grid(网格)对象 C, Grid(网格)对象 ...
- .NET组件编程
链接 http://www.cnblogs.com/mapserver/archive/2006/03/06/343632.html
- PS 如何制作眼泪效果
1.用钢笔工具勾出眼泪的路径然后按Ctrl + Enter转为选区 2.按Ctrl + J 把选区复制出来,执行滤镜 > 扭曲 > 球面化 同样的方法制作流出的眼泪,然后添加图层样式选择投 ...
- Azure Mobile App - Custom Authentication
Custom Authentication: 1. For OLD Mobile Service - https://azure.microsoft.com/en-us/documentation/a ...