当前位置: 代码迷 >> 综合 >> spring中读取配置文件中自定义变量
  详细解决方案

spring中读取配置文件中自定义变量

热度:92   发布时间:2024-01-26 07:07:13.0

有两种方法

一、@Value(“${xxx.xxxx}”),在需要使用的地方用value注解

yml中mypop:value1: abc代码中
@Value("${mypop.value1}")
String value ;@Testpublic void test8(){System.out.println(value);}

二、定义一个配置文件类,利用@ConfigurationProperties(prefix = "weixinAndAli")注解

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>
@Component
@ConfigurationProperties(prefix = "mypop")
public class ConfigUtils {private String vaule;//get  set  方法补全}

在使用的地方注入即可

public class xxx{@autowaerpublic ConfigUtil configUtil;public void test(){String s = configUtil.value;}}

 

  相关解决方案