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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hero_孙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值