SpringBoot 属性配置中获取值

在配置文件中定义属性的值,然后再获取,在这里做一个总结,首先,在application.yml文件中定义端口和属性的值,当然,在.application.properties文件中也能定义,只是格式不同而已:

appliaction.yml:

server:
  port: 8081
cubSize: B
age: 18

然后再定义一个controller,用@Value这个注解来获取到值:

package com.dist.tr.controller;

import com.dist.tr.entity.GrilProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping
public class BeautifulGirlContrller {

    @Value("${cubSize}")
    private String cubSize;

    @Value("${age}")
    private Integer age;

    @RequestMapping(value = "/gril",method = RequestMethod.GET)
    public String HelloGril(){
        return cubSize + age;
    }


}

运行结果:
这里写图片描述

如果当属性很多之后,要写很多的@Value 的注解嘛???答案当然是No,有简便的写法:

application.yml 文件

server:
  port: 8081
gril:
  name: lisa
  height: 165

首先,定义一个实体类去写属性:
GrilProperties实体:
注意我们用到了这个注解:@ConfigurationProperties(prefix = “gril”)

package com.dist.tr.entity;

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

@Component
@ConfigurationProperties(prefix = "gril")
public class GrilProperties {

    private String name;
    private String height;

    public String getName() {
        return name;
    }

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

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }
}

在controller 中的写法:
首先用注解@Autowired 注入这个实体,如果不在实体中加@Component这个注解,在idea中发现会有红线出现。

package com.dist.tr.controller;

import com.dist.tr.entity.GrilProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping
public class BeautifulGirlContrller {

    @Autowired
    private GrilProperties grilProperties;

    @RequestMapping("/grilPerproties")
    public String grilPerproties(){
        return grilProperties.getName()+grilProperties.getHeight();
    }


}

运行结果:

这里写图片描述

这样就不会需要去写太多的@Value注解了。
还有中形式,就是在配置文件中也可以有这种情况出现:

server:
  port: 8081
cubSize: B
age: 18
context: "cubSize:${cubSize},age:${age}"

这种情况证明获取的属性值呢?

在controller中编码:

package com.dist.tr.controller;

import com.dist.tr.entity.GrilProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping
public class BeautifulGirlContrller {

    @Value("${context}")
    private String context;


    @RequestMapping("/grilSize")
    public String girlcubSize(){
        return context ;
    }
}

运行结果:
这里写图片描述

2、测试和生产区分:

当在项目开发的时候,如果区分测试和生产环境,那么就得区分开application.yml 文件:

新建application-dev.yml 文件和application-prod.yml文件:
,然后在使用测试或者是生产的时候,application.yml 文件中这样写:

spring:
  profiles:
    active: prod

决定是用测试环境还是生产环境。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值