IDEA的使用(2 springboot 配置文件)

使用IDEA 创建springboot 并实现helloworld  

详情-> https://blog.csdn.net/aa15237104245/article/details/81002962

springboot 的配置文件 之  application.properties

文件路径为:


可以配置的内容有:

自定义内容

com.shj.name=shj
com.shj.age=25

可以使用@Value调用配置文件中的值

@Value("${com.shj.name}")
private  String name;
@Value("${com.shj.age}")
private  String age;
@RequestMapping("/name")
public  String name(){

    return  name +age ;
}

然后可以查看结果


如果有多个类似的配置

com.shj.name=shj
com.shj.age=25
com.shj.sex=com.shj.location=上海
com.shj.job=程序猿

此时可以使用一个对象去绑定

package com.example.demo.domain;

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

/**
 * Created by shj on 2018/7/12.
 */

@ConfigurationProperties(prefix = "com.shj")
public class User {
    private String name;
    private String age;
    private String sex;
    private String location;
    private String job; //getter,setter,tiString() 方法}

@Resource
private  User users;

@RequestMapping("/user")
public String user(){
    return users.toString();
}

此时会报错,提示无法自动加载User

需要在入口处加上注解 

@EnableConfigurationProperties({User.class})

然后运行项目 ,出现了乱码


application.properties中添加配置信息

spring.http.encoding.charset=UTF-8

配置文件间可相互引用

app.name=MyApp
app.description=${app.name} is a Spring Boot application

随机数

# 随机字符串
com.test.value=${random.value}
# 随机int
com.test.number=${random.int}
# 随机long
com.test.bignumber=${random.long}
# 10以内的随机数
com.test.test1=${random.int(10)}
# 10-20的随机数
com.test.test2=${random.int[10,20]}
我测试了下,每次运行项目,随机值会发生变动,但是项目启动后,刷新页面随机值不变


配置一些非自定义的信息,即springboot支持的一些配置

#端口号
server.port=8081
#session失效时间
server.session-timeout=30
#编码
server.tomcat.uri-encoding=utf-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8



猜你喜欢

转载自blog.csdn.net/aa15237104245/article/details/81011329