SpringBoot属性配置

SpringBoot属性配置

Spring Boot并不真正是所谓的『零配置』,他的理念是“习惯优于配置”采用了一些默认的习惯性配置,让你无需手动进行配置,从而让你的项目快速运行起来。所以要想玩转Spring Boot,了解这些默认配置还是必不可少的。

创建Spring Boot项目时,会默认生成一个全局配置文件application.properties(可以修改后缀为.yml),在src/main/resources目录下或者类路径的/config下。我们可以通过修改该配置文件来对一些默认配置的配置值进行修改。
修改默认配置
1、spring boot 开发web应用的时候,默认tomcat的启动端口为8080,如果需要修改默认的端口以及修改访问路径则需要在application.yml添加以下记录:

server:
  port: 8888
  servlet:
    context-path: /wjy

输入路径
在这里插入图片描述
2、自定义属性及读取
我们可以在application.yml文件中,配置一些常量或者其他参数配置。读取的时候通过Spring的@Value(“${属性名}”)注解即可。
application.yml文件中配置:

server:
  port: 8888
  servlet:
    context-path: /wjy
server_ip: 1.1.1.1
server_port: 2345

实现类GetIp中:

package com.offcn.springbootdemo1.test;

import com.offcn.springbootdemo1.po.UserBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GetIp {

    @Value("${server_ip}")
    private String server_ip;
    @Value("${server_port}")
    private String server_port;

    @RequestMapping("/ip")
    public String getServer_ip(){
        return "ip--"+server_ip+";"+server_port;
    }
}

3、实体类属性赋值
当属性参数变多的时候,我们习惯创建一个实体,用实体来统一接收赋值这些属性。
1).定义配置文件

server:
  port: 8888
  servlet:
    context-path: /wjy

userbody:
  age: 14
  name: wangjiyu
  hobby: java

2).创建实体类:
加注解并指定prefix前缀@ConfigurationProperties(prefix = “userbody”)

package com.offcn.springbootdemo1.po;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "userbody")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserBody {
    private Integer age;
    private String name;
    private String hobby;
}

3)、编写Controller调用属性bean
加入注解@EnableConfigurationProperties({UserBody.class})
并在注解中引入实体类

package com.offcn.springbootdemo1.test;

import com.offcn.springbootdemo1.po.UserBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableConfigurationProperties({UserBody.class})
public class GetIp {
    //实体类赋值
    @Autowired
    UserBody userBody;
    @RequestMapping("/user")
    public String getUser(){
        return userBody.toString();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值