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

网友投稿 1706 2023-01-10

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

@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 maps;

private List 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()获取不到属性。

classpath:queryScoreIPList.properties

location="classpath:queryScoreIPList.properties" />

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

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

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

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

@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所在类和PropertySourcesPlaceholdaEgwQferConfigurer装配顺序无关),并且@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:property-placeholder location="${location}"/>

propList.properties

location:classpath:queryScoreIPList.properties

location="classpath:queryScoreIPList.properties" />

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

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

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

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

@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所在类和PropertySourcesPlaceholdaEgwQferConfigurer装配顺序无关),并且@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:property-placeholder location="${location}"/>

propList.properties

location:classpath:queryScoreIPList.properties

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序生态前端(小程序 生态)
下一篇:企业app开发要多少钱(公司开发一个app需要多少钱)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~