@PropertySource 无法读取配置文件的属性值解决方案

原来Person类这样写: 只写了@PropertySource注解

@Component
@PropertySource(value = {"classpath:person.properties"})
public class Person {

    private String lastName;
    private int age;
    private boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
...
}

运行后找不到配置文件中的值:

解决方法:

加上@ConfigurationProperties注解:

@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})
public class Person {

运行,获取到配置文件中的值:

为啥呢??

因为 @ConfigurationProperties(prefix = “person”)表示该类的属性值为配置中的属性值,找前缀为person的属性。

首先从全局配置文件中找是否有person对应的属性值,如果有那么就输出全局配置中的属性值;如果没有,@PropertySource意思是属性来源,从@PropertySource指定的路径中找到对应的配置文件,进行赋值。

@Value和@PropertySource读配置文件采坑留念

一、 网上查到的方式:

直接 @PropertySource加载文件@Value读取属性,

Environment.getProperty()获取属性。

结果发现@Value只能拿到"${ips}",获取不到配置文件里的属性。

@Controller
@PropertySource("classpath:queryScoreIPList.properties")
public class UserController {
 @Value("${ips}")
 private String ips;
 @Autowired
 private Environment environment;

 public void user() {
  System.err.println(ips);
 System.err.println(environment.getProperty("ips"));
 }
}

二、 其他方式:

两种方式加载配置文件,

@Value正常获取属性,

Environment.getProperty()获取不到属性。

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:queryScoreIPList.properties</value>
   </list>
  </property>
</bean> 
<context:property-placeholder ignore-unresolvable="true"
  location="classpath:queryScoreIPList.properties" /> 

三、非要@PropertySource+@Value也可以:

spring配置里装配PropertySourcesPlaceholderConfigurer或配置context:property-placeholder,

代码里@PropertySource加载配置文件,

@Value获取属性,Environment.getProperty()正常获取属性。

<!--  <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"/>  -->
<context:property-placeholder/>
@Controller
@PropertySource("classpath:queryScoreIPList.properties")
public class UserController {
 @Value("${ips}")
 private String ips;

 public void user() {
  System.err.println(ips);
 }
}

总结:

1、@Value从PropertySourcesPlaceholderConfigurer类的PropertySources集合中获取属性。

2、PropertySourcesPlaceholderConfigurer初始化时会将Environment作为PropertySource放到集合中。

3、@PropertySource注解只加载配置文件到Environment。

4、启动时@PropertySource注解初始化早于PropertySourcesPlaceholderConfigurer(与@PropertySource所在类和PropertySourcesPlaceholderConfigurer装配顺序无关),并且@PropertySource加载的配置在xml文件中可以正常获取。

com.controller.user.UserController.java

@Controller
@PropertySource(value="classpath:propList.properties")
public class UserController {
 @Value("${ips}")
 private String ips;
 public void user() {
  System.err.println(ips);
  System.err.println(environment.getProperty("ips"));
 }
}

applicationContext.xml

<context:component-scan base-package="com.dao"/>
 <context:component-scan base-package="com.controller"/>
  <context:property-placeholder location="${location}"/>  

propList.properties

location:classpath:queryScoreIPList.properties

以上代码可以正常获取queryScoreIPList.properties文件中的ips属性。希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Spring Boot自定义配置属性源(PropertySource)

    配置覆盖优于profile 在生产实践中,配置覆盖是解决不同环境不同配置的常用方法.比如用生产服务器上的配置文件覆盖包内的文件,或者使用中心化的配置服务来覆盖默认的业务配置. 相比于profile机制(比如maven的profile.spring boot的profile-specific properties),即不同环境使用不同的配置文件,覆盖的方式更有优势.程序员在开发时不需要关心生产环境数据库的地址.账号等信息,一次构建即可在不同环境中运行,而profile机制需要将生产环境的配置写到项

  • 在SpringBoot下读取自定义properties配置文件的方法

    SpringBoot工程默认读取application.properties配置文件.如果需要自定义properties文件,如何读取呢? 一.在resource中新建.properties文件 在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下.如图remote.properties所示 二.编写配置文件 remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource

  • spring无法读取properties文件数据问题详解

    1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-context.xml配置文件中获取的:service中注入的@Value配置可以从applicationContext.xml中获取的.所以,如果要在controller中注入属性配置,需要在相应servlet文件中添加配置,同applicationContext.xml中一样. <bean class="org.springframework.be

  • 解决springboot利用ConfigurationProperties注解配置数据源无法读取配置信息问题

    @ConfigurationProperties是springboot新加入的注解,主要用于配置文件中的指定键值对映射到一个java实体类上.那么它是怎么发挥作用的呢?下面我们将揭开@ConfigurationProperties的魔法. ConfigurationPropertiesBindingPostProcessor这个bean后置处理器,就是来处理bean属性的绑定的,这个bean后置处理器后文将称之为properties后置处理器.你需要知道以下几件事: ioc容器context的e

  • @PropertySource 无法读取配置文件的属性值解决方案

    原来Person类这样写: 只写了@PropertySource注解 @Component @PropertySource(value = {"classpath:person.properties"}) public class Person { private String lastName; private int age; private boolean boss; private Date birth; private Map<String,Object> map

  • java springboot中如何读取配置文件的属性

    目录 配置文件 (1)使用注解@Value映射 (2)使用@ConfigurationProperties映射 (3)推荐使用:极简方式 @Bean和@ConfigurationProperties注解一起使用, @RequiredArgsConstructor注解实现自动注入 总结 我们知道在比较大型的项目的开发中,比较经常修改的属性我们一般都是不会在代码里面写死的,而是将其定义在配置文件中,之后如果修改的话,我们可以直接去配置文件中修改,那么在springboot的项目中,我们应该如何实现这

  • C#实现读取匿名对象属性值的方法示例总结

    本文实例讲述了C#实现读取匿名对象属性值的方法.分享给大家供大家参考,具体如下: 通过new出匿名对象,可以直接调用该匿名对象的属性名,获取属性值. var objUser = new {Name="Lilei",Age=18 }; //此时可直接读取匿名类属性 Console.WriteLine("Name:" + objUser.Name);// Name:Lilei 但当将匿名对象转换成object后,就无法直接读取属性值了: static object Ge

  • springboot如何读取配置文件(application.yml)中的属性值

    在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: <!-- 支持 @ConfigurationProperties 注解 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId>

  • springboot读取自定义配置文件时出现乱码解决方案

    目录 网上提供的解决方案也不外乎这几种 方案一 方案二 方案三 方案四 方案五 方案六 这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子.接下来当然是读取简单的对象属性了. 于是按照网上各位大神教的,简单写了个对象book,其他配置不需要做任何改动. package com.example.bean; import org.springframework.beans.factory.annotation.Value; import org.springframe

  • 使用@PropertySource读取配置文件通过@Value进行参数注入

    目录 @PropertySource读取配置文件通过@Value参数注入 Spring读取配置@Value.@PropertySource.@ConfigurationProperties使用 @Value @Value中$和#的区别 @PropertySource:加载配置属性源 @ConfigurationProperties @PropertySource读取配置文件通过@Value参数注入 有参数文件如下test.properties project.author=wpfc projec

  • C#通过XML节点属性/属性值读取写入XML操作代码实例

    1.XML的内容如下: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8" ?><root>  <title>    <settings id = "0" name = "显示文字">欢迎您!智慧服务,互动体验......</settings>    <settings id = "1" name

  • C#通过属性名称获取(读取)属性值的方法

    之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊! 然后今天我再上网找了找,被我找到了,跟大家分享一下. 其实原理并不复杂,就是通过反射利用属性名称去获取属性值,以前对反射不熟悉,所以没想到啊~ 不得不说反射是一种很强大的技术.. 下面给代码,希望能帮到有需要的人. using System; using System.Collections.Generic; using System.Linq; using Sys

  • Spring中利用配置文件和@value注入属性值代码详解

    1 简单属性值注入 package com.xy.test1; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service // 需要被注入属性值的类需要被Spring管理 public class PropertiesService1 { // 利用@Value注解,即使没有该属性或者属性文件也不会报错 // @Value输入

  • C#读取静态类常量属性和值的实例讲解

    1.背景 最近项目中有一个需求需要从用户输入的值找到该值随对应的名字,由于其它模块已经定义了一份名字到值的一组常量,所以想借用该定义. 2.实现 实现的思路是采用C#支持的反射. 首先,给出静态类中的常量属性定义示例如下. public static class FruitCode { public const int Apple = 0x00080020; public const int Banana = 0x00080021; public const int Orange = 0x000

随机推荐