在springBoot的静态方法中,如何引用配置文件yml中的参数

在springBoot项目中,我们一般把参数配置到application.yml中,但是如果想在工具类的静态方法中使用这些参数,常用的三种方式:@value 、@ConfigurationProperties或 Environment 都没法取到值。

因此需要使用@PostConstruct 注解来实现

1、在application.yml中加入以下配置

server:
  port: 10091
  servlet:
    context-path: /spring-boot-demo

read:
  type:
    T1: first
    T2: second
    T3: third

2、创建一个工具类 PropertiesUtil.java

package com.demo.springbootdemo.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @program: spring-boot-demo
 * @author: MingYi
 * @date: 2021/7/15 17:59
 * @description: 属性文件获取
 **/
@Component
public class PropertiesUtil {
    @Autowired
    private Environment environment;
    private static Environment env;

    //PostConstruct注解不可以有参数的
    @PostConstruct
    public void init(){
        env = this.environment;
    }

    public static String getType(String id){
        String key = "read.type.T"+id;
        String type = env.getProperty(key);
        return type;
    }

}

3、在Controller或是其他地方直接使用此工具类即可:UtilController.java

package com.demo.springbootdemo.controller;

import com.demo.springbootdemo.util.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: spring-boot-demo
 * @author: MingYi
 * @date: 2021/7/15 19:50
 * @description: 验证类
 **/
@Slf4j
@RestController
public class UtilController {

    @PostMapping("/test/getType")
    public void getType(@RequestBody Map<String,String> reqMap){
        String id = reqMap.get("id");
        log.info("获取的类型:{}", PropertiesUtil.getType(id));
    }
}

注意点:

1、这个解决方案的核心是@PostConstruct ,如果出现以下错误: java.lang.IllegalStateException: Lifecycle method annotation requires a no-arg method   

这是因为使用此注解的方法 不可以有参数

2、使用@value @ConfigurationProperties 也是相同的思路

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot提供了多种方式来存放静态参数配置文件。 1. 在application.properties/application.yml文件存放静态参数。这是Spring Boot默认的配置文件,可以在其定义各种静态参数。例如,可以在application.properties文件定义数据库连接参数: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=myusername spring.datasource.password=mypassword ``` 2. 在多个配置文件同时存放静态参数。除了默认的application.properties文件,Spring Boot还提供了更多的配置文件来存放静态参数。可以使用`@PropertySource`注解加载其他配置文件,并在其定义静态参数。例如,可以创建一个custom.properties文件,并在其定义自定义的静态参数: ```java @Configuration @PropertySource("classpath:custom.properties") public class AppConfig { @Value("${custom.property}") private String customProperty; // ... } ``` 3. 使用@Value注解直接将静态参数注入到代码。可以使用@Value注解将静态参数注入到Spring管理的Bean。例如,可以在一个Service类注入静态参数: ```java @Service public class MyService { @Value("${custom.property}") private String customProperty; // ... } ``` 4. 使用外部配置文件存放静态参数。除了内部的配置文件,Spring Boot还支持使用外部的配置文件存放静态参数。可以通过命令行参数、环境变量等方式指定外部的配置文件。例如,可以通过`--spring.config.location`参数指定一个外部的application.properties文件: ``` java -jar myproject.jar --spring.config.location=/path/to/application.properties ``` 总之,通过以上方式,我们可以方便地存放和管理Spring Boot应用程序的静态参数配置文件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值