springboot配置

springboot配置文章目录springboot配置1.springboot加载配置顺序2.springboot加载自定义属性3.springboot内置web容器的常用配置环境springboot1.5.91.springboot加载配置顺序1.1 springboot启动会扫描一下位置的application.properties或者application.yml作为默认的配...
摘要由CSDN通过智能技术生成

springboot配置


环境

springboot1.5.9

1.springboot加载配置顺序

1.1 springboot启动会扫描一下位置的application.properties或者application.yml作为默认的配置文件
工程根目录:./config/
工程根目录:./
classpath:/config/
classpath:/
加载的优先级顺序是从上向下加载,并且所有的文件都会被加载,高优先级的内容会覆盖底优先级的内容,形成互补配置

也可以通过指定配置spring.config.location来改变默认配置,一般在项目已经打包后,我们可以通过指令 
  java -jar xxxx.jar --spring.config.location=/**/application.yml来加载外部的配置

2.springboot加载自定义属性

三种方式,见下面示例

bean映射

@Component
//指定配置路径
@PropertySource("classpath:conf/user.properties")
@ConfigurationProperties(prefix="test")
public class UserInfo2 {
    private String userName;
    private String passWord;
    ......
}

使用

@RestController
public class TestController {
    @Value("${com.ctg.test.userName}")
    private String userName;
    @Value("${com.ctg.test.passWord}")
    private String passWord;
    @Autowired
    private Environment environment;
    @Autowired
    private UserInfo userInfo ;
    @Autowired
    private UserInfo2 userInfo2 ;
    
    @RequestMapping(value = "/test")
    @ResponseBody
    public Object test(){
        Map<String,Object>map =new HashMap<>();
        map.put("code",200);
        map.put("@Value读取:",userName+":"+passWord);
        map.put("environment读取:",environment.getProperty("com.ctg.test.userName")+":"+environment.getProperty("com.ctg.test.passWord"));
        map.put("Bean读取:",userInfo.getUserName()+":"+userInfo.getPassWord());
        map.put("Bean读取指定配置路径:",userInfo2.getUserName()+":"+userInfo2.getPassWord());
        return map;
}
}

3.springboot内置web容器的常用配置

参考类的属性进行配置:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
在这里插入图片描述

# -----------------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值