spring boot实战(第四篇)分散配置

spring boot实战(第四篇)分散配置

前言

分散配置是系统必不可少的一部分,将配置参数抽离出来为后期维护提供很大的便利。spring boot 默认支持两个格式的配置文件:.properties .yml。

.properties与.yml

*.properties属性文件;属于最常见的一种;
*.yml是yaml格式的文件,yaml是一种非常简洁的标记语言。

在*.properties中定义user.address.stree=hangzhou等价与yaml文件中的

   user:
         address: 
                 stree:hangzhou

从上可以发现yaml层次感更强,具体在项目中选择那种资源文件是没有什么规定的。

spring boot配置

简单案例

首先在类路径下创建application.properties文件并定义
name=liaokailin

创建一个beanUser.java

@Component
public class User {

    private @Value("${name:lkl}") String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

HelloWorldController.java调用对应bean

package com.lkl.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.lkl.springboot.config.User;
import 
  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值