前言

只是简单的properties配置学习,修改部分“约定”改为自定义“配置”。真正使用和遇到问题是在细看。

一、主要

核心只是demo中的: @PropertySource(value = "classpath:/config/custom.properties", ignoreResourceNotFound = true)

二、demo

// application 注解
@Configuration
@ComponentScan
@EnableAutoConfiguration
@PropertySource(value = "classpath:/config/custom.properties", ignoreResourceNotFound = true)
// Controller 注解
@Controller
public class PropertiesController {
@Value("${CONSTANT_PASSWORD}")
private String password; @Autowired
private ConfigBean configBean; @RequestMapping("/custom")
public String custom(@RequestParam(value="name", required=false, defaultValue="${CONSTANT_USER}") String name
, Model model) {
model.addAttribute("name", name);
model.addAttribute("password", password);
System.out.println(configBean);
return "custom";
} public static void main(String[] args) {
SpringApplication.run(PropertiesController.class, args);
}
}

config/custom.properties

## 常量配置
CONSTANT_USER=vergiyln
CONSTANT_PASSWORD=中文123 ## thymeleaf 配置
spring.thymeleaf.prefix=classpath:/templates/properties/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache=false

config/ConfigBean.properties  注入bean配置(日期暂时不知道怎么注入)

vergilyn.map[blog]=http://www.cnblogs.com/VergiLyn/
vergilyn.map[name]=VergiLyn
vergilyn.map[remark]=备注,中文23333 vergilyn.list[0]=Carpenters
vergilyn.list[1]=Celine Dion
vergilyn.list[2]=Bon Jovi
vergilyn.list[3]=Taylor Swift vergilyn.str=string
vergilyn.num=124
vergilyn.date=2017-01-14 23:55:19
vergilyn.isSuccess=false

properties对应的JavaBean

@Configuration
// prefix = value,看源码
@ConfigurationProperties(prefix = "vergilyn")
@PropertySource("classpath:config/ConfigBean.properties")
@Component
public class ConfigBean implements Serializable{
private String str; private Integer num; // 注意:要是setIsSuccess(),不能是setSuccess() private boolean isSuccess;
// 日期不知道怎么注入// private Date date; private Map<String, String> map; private List<String> list; public String getStr() {
return str;
} public void setStr(String str) {
this.str = str;
} public Integer getNum() {
return num;
} public void setNum(Integer num) {
this.num = num;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public Boolean isSuccess() {
return isSuccess;
} public void setIsSuccess(Boolean success) {
isSuccess = success;
}
}

custom.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>测试properties</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'user: ' + ${name}" />
<p th:text="'password: ' + ${password}" />
</body>
</html>

测试结果:

二、扩展

1、idea中注入properties中文乱码。

起初,我以为需要*.properties或者*.xml或者别的什么配置中要配置编码,但发现并不是。

参考:Springboot 之 解决IDEA读取properties配置文件的中文乱码问题

2、注入属性类型。

如demo,基本上string、int是可以的,不用多余的配置。但测试date并不知道怎么注入。

特别需要注意的是isSuccess;生成的set是setSuccess(),但properties中确实isSuccess导致注入失败。

3、@ConfigurationProperties中可以设置前缀

Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
@AliasFor("prefix")
String value() default ""; @AliasFor("value")
String prefix() default ""; boolean ignoreInvalidFields() default false; boolean ignoreNestedProperties() default false; boolean ignoreUnknownFields() default true; boolean exceptionIfInvalid() default true; /** @deprecated */
@Deprecated
String[] locations() default {}; /** @deprecated */
@Deprecated
boolean merge() default true;
}

如源码,可以看出prefix与value是一样的。

4、thymeleaf 表达式、方言

${...}:变量表达式;

*{...}:选择变量表达式;

#{...}:消息表达式;

@{...}:链接url表达式

有待了解,现在还不清楚具体是什么、如何使用、什么时候用。

参考:

thymeleaf 基本语法

5分钟了解Thymeleaf的标准方言(Standard dialects)

附录

玩转spring boot——properties配置

【spring boot】SpringBoot初学(2) - properties配置和读取的更多相关文章

  1. Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)

    Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...

  2. Spring Boot 的配置文件application.properties

    Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...

  3. Spring boot运行原理-自定义自动配置类

    在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...

  4. Spring Boot加载application.properties配置文件顺序规则

    SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...

  5. Spring Boot应用的后台运行配置

    酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...

  6. Spring Boot 启动(二) 配置详解

    Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...

  7. spring boot中的约定优于配置

    Spring Boot并不是一个全新的框架,而是将已有的Spring组件整合起来. Spring Boot可以说是遵循约定优于配置这个理念产生的.它的特点是简单.快速和便捷. 既然遵循约定优于配置,则 ...

  8. JAR(Spring Boot)应用的后台运行配置

    酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...

  9. Spring Boot应用的后台运行配置(转载)

    作者:程序猿DD 酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用 ...

  10. [转]Spring Boot应用的后台运行配置

    转自:http://blog.didispace.com/spring-boot-run-backend/ 酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前, ...

随机推荐

  1. Ubuntu14.04下安装redis

    1.首先在官网上下载redis压缩包 redis-3.2.0.tar.gz 2.解压到到当前文件夹(这里可以解压到随意位置) tar zvxf redis-3.2.0.tar.gz 3.切换到redi ...

  2. 2015 ACM/ICPC Asia Regional Shanghai Online

    1001 Puzzled Elena 1002 Antonidas 1003 Typewriter 1004 Count the Grid 1005 Code Formatting 1006 Ther ...

  3. BFS详解

    广度优先搜索详解          1. 也称宽度优先搜索,顾名思义,就是将一棵树一层一层往下搜.算法首先搜索和s距离为k的所有顶点,然后再去搜索和S距离为k+l的其他顶点.BFS是一种完备策略,即只 ...

  4. void与void*详解

    void关键字的使用规则: 1. 如果函数没有返回值,那么应声明为void类型: 2. 如果函数无参数,那么应声明其参数为void: 3. 如果函数的参数可以是任意类型指针,那么应声明其参数为void ...

  5. pig、hive以及hbase的作用

    Pig Pig是一种数据流语言,用来快速轻松的处理巨大的数据.Pig包含两个部分:Pig Interface,Pig Latin.Pig可以非常方便的处理HDFS和HBase的数据,和Hive一样,P ...

  6. Hibernate与Sleep的区别

    转自:http://blog.sina.com.cn/s/blog_4b6e98810100n37k.html 休眠(Hibernate),将系统切换到该模式后,系统会自动将内存中的数据全部转存到硬盘 ...

  7. linux上安装配置samba服务器

    linux上安装配置samba服务器 在linux上安装配置samba服务器 在这给大家介绍一个不错的家伙,samba服务.如果您正在犯愁,如何在Windows和Linux之间实现资源共享,就请看看这 ...

  8. leetcode387

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  9. 用php和imagemagick来处理图片文件的上传和缩放处理

    啥也不说,直接上代码,大家可以自行添加增加水印功能: <?php /** * * @author zhao jinhan * @date 2014年1月13日11:54:30 * @email  ...

  10. 十一章:用CSS进行布局

    本章重点:盒模型与元素浮动. 盒模型: 1.CSS处理网页时,它认为每个元素都包含在一个不可见的盒子里.这就是众所周知的盒模型,这里的盒子由内容区域.内容区域周围的空间.内边距和外边缘和边框外面将元素 ...