Springboot读取application.properties配置

Springboot读取application.properties配置文件的值

springboot 读取application.properties配置中信息的三种方法, 分别如下:

  1. 使用@Value读取配置;
  2. 注入Environment 对象,并通过Environment的getProperty 方法获取配置信息的值;
  3. 使用@ConfigurationProperties(prefix = “com.user”)获取application.properties配置信息前缀;

application.properties自定义配置信息如下:

# 自定义配置
com.tahacoo.username=admin
com.tahacoo.password=123456
com.tahacoo.level=1
com.tahacoo.url=www.tahacoo.com

使用@value注释

使用@value:可以注入具体的配置信息。

优点:单个使用方便,灵活性高,适合获取简单的配置信息。

缺点:获取配置文件信息多,需要些大量的@Value,不利于开发,增加开发者的工作量;

代码示例如下:

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

@RestController
public class UserController {
    @Value("${com.tahacoo.username}")
    private String name;  // 读取配置文件中的name属性
    
    @GetMapping("/getName")
    public String getName() {
        return this.name;
    }
}

注入Environment对象

注入Environment 对象,并通过Environment的getProperty 方法获取配置信息的值;

优点:可以获取配置文件中的所有信息,方便开发,减少工作量;

缺点:获取配置文件信息多,需要些大量的@Value,不利于开发,增加开发者的工作量;

代码示例如下:

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import jakarta.annotation.Resource;

@RestController
public class UserController {
    @Resource
    private Environment environment;

    @GetMapping("/getLevel")
    public Integer getAge() {
        return environment.getProperty("com.tahacoo.level", Integer.class);
    }
}

使用@ConfigurationProperties注释

使用@ConfigurationProperties(prefix = “com.user”)获取application.properties配置信息前缀;

优点:可以获取配置文件中的所有信息,方便开发,减少工作量;

注意:需要在配置类上添加@ConfigurationProperties注解,并且需要在配置类上添加@Component注解,才能生效。

配置类如下:

package com.tahacoo.exercise.properties;

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

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Component
@ConfigurationProperties(prefix = "com.tahacoo")
public class User {
    private String username;
    private String password;
    private Integer level;
    private String url;
}
      

调用代码如下:

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import jakarta.annotation.Resource;
import com.tahacoo.exercise.properties.User;

@RestController
public class UserController {
    @Resource
    private User user;

    @GetMapping("/getUser")
    public String getUser() {
        return user.toString();
    }
}

输出结果如下:

User(username=admin, password=123456, level=1, url=www.tahacoo.com)

注入Properties对象

注入Properties对象,并通过Properties的getProperty 方法获取配置信息的值;

该方式适合适用于非SpringBoot项目,需要在application.properties文件中配置信息。

#value值不要有双引号("")和分号(;)
jdbc.url=jdbc:mysql://xxxx:3306/yygh_user
jdbc.username=root
jdbc.password=xxxxxx

代码示例如下:

package com.basic.jdbc.util;
import java.util.Properties;

public class JDBCUtil {
    public static Connection getConn(){
        Properties properties = new Properties();
        try {
            //使用 JDBCUtil 类的类加载器获取 application.properties 文件的输入流。getResourceAsStream 方法会在类路径下查找指定名称的文件,若找到则返回其输入流。
            InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("application.properties");
            // 调用 Properties 对象的 load 方法,将输入流中的属性加载到 properties 对象中。该方法会解析输入流中的键值对,格式通常为 key=value。
            properties.load(resourceAsStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
    }
    
}

参考链接:

  1. https://blog.csdn.net/weixin_42344117/article/details/118087641
posted @ 2025-06-22 22:29  joudys  阅读(246)  评论(0)    收藏  举报