@Value 读取yml 文件

首先,@Value需要参数,这里参数可以是两种形式:

  • @Value("#{configProperties['t1.msgname']}")
  • @Value("${t1.msgname}");

我项目中使用@Value注解读取yml文件中的配置

使用方式:

  @Value("${tag}")
  private String value

value 值为空的原因:

  • 使用static或final修饰了tagValue,如下:
    private static String value;  //错误
    private final String value;    //错误
  • 类没有加上@Component(或者@service等)
 @Component   //遗漏
    class TestValue{
         @Value("${tag}")
         private String value;
    }
  • 使用@Autowired而不能通过new创建实例
class Test{
        @AutoWired
        TestValue testValue //正确
         ......
        TestValue test = new TestValue();//错误
   }



作者:Java_Rock
链接:https://www.jianshu.com/p/52b28c264a7c
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

转载于:https://my.oschina.net/u/3358860/blog/3078999

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用yaml库来读取yml文件并使用@Value注释将其注入到Spring Boot应用程序中。首先,您需要在pom.xml文件中添加yaml依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.5.4</version> </dependency> ``` 然后,在您的Spring Boot应用程序中,您可以使用以下代码来读取yml文件并将其注入到应用程序中: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource(value = "classpath:application.yml", factory = YamlPropertySourceFactory.class) public class YamlConfig { @Value("${property.key}") private String propertyValue; public String getPropertyValue() { return propertyValue; } } ``` 在上面的代码中,我们使用@PropertySource注释将yml文件路径指定为"classpath:application.yml",并使用YamlPropertySourceFactory类来加载yml文件。然后,我们使用@Value注释将yml属性注入到应用程序中。 请注意,YamlPropertySourceFactory类是一个自定义工厂类,用于将yml文件转换为属性源。以下是该类的代码: ```java import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.support.PropertySourceFactory; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; import java.io.IOException; import java.util.Properties; public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { Resource yamlResource = new ClassPathResource("application.yml"); Properties propertiesFromYaml = loadYamlIntoProperties(yamlResource); String sourceName = name != null ? name : resource.getResource().getFilename(); return new PropertiesPropertySource(sourceName, propertiesFromYaml); } private Properties loadYamlIntoProperties(Resource yaml) throws IOException { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(yaml); Properties properties = factory.getObject(); return properties != null ? properties : new Properties(); } } ``` 这样,您就可以使用@Value注释将yml文件中的属性注入到Spring Boot应用程序中了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值