SpringBoot项目——读取配置文件及自定义配置文件

本文介绍了如何在SpringBoot项目中读取默认配置文件和自定义配置文件。首先讲解了通过application.properties读取属性,并展示了注解@Value的使用。接着详细阐述了创建和绑定自定义配置文件的步骤,包括创建配置文件、将配置绑定到实体类以及在控制器中注册实体类的实例。通过访问特定URL可查看操作结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、读取默认配置文件

1、application.properties:

#端口号
server.port=9090
#自定义属性
test.msg=hello

2、用注解@Value读取属性

package com.gui.hello;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class HelloController {
    @Value("${test.msg}")
    private String msg;

    @RequestMapping("/hello")
    public String hello(){
        return msg;
    }
}

3、访问: http://localhost:9090/test/hello
结果:
在这里插入图片描述

二、读取自定义配置文件

1、步骤

1. 创建自定义配置文件
2. 将配置绑定到实体类

@Configuration //将配置文件与实体类绑定
@PropertySource(value = "classpath:/config/userinfo.properties") //指明配置文件的位置
@ConfigurationProperties(prefix = "userinfo") //指明配置文件的名称

3. 在控制器中注册实体类

@EnableConfigurationProperties(UserInfo.class) //注册实体类
2、实例

1. 在resources/config目录下创建配置文件userinfo.properties:

userinfo.name=xiaoming
userinfo.age=25
userinfo.city=HangZhou

2. 将配置绑定到实体类

UserInfo.java

package com.gui.hello.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 用户信息类
 */
@Configuration //将配置文件与实体类绑定
@PropertySource(value = "classpath:/config/userinfo.properties") //指明配置文件的位置
@ConfigurationProperties(prefix = "userinfo") //指明配置文件的名称
public class UserInfo {
    private String name;
    private int age;
    private String city;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}

3. 控制器中注册实体类

UserInfoController.java

package com.gui.hello.web;

import com.gui.hello.domain.UserInfo;
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 //结果返回json
@RequestMapping("/userinfo")
@EnableConfigurationProperties(UserInfo.class) //注册实体类
public class UserInfoController {
    @Value("${userinfo.name}")
    private String name;

    @Value("${userinfo.age}")
    private int age;

    @Value("${userinfo.city}")
    private String city;

    @RequestMapping("/hello")
    public String hello(){
        return "我的名字叫"+name+",我今年"+age+"岁了,我住在"+city;
    }
}

4、运行结果
访问:http://localhost:9090/userinfo/hello
结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值