java – @Value无需配置静态PropertySourcesPlaceholderConfigurer即可运行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – @Value无需配置静态PropertySourcesPlaceholderConfigurer即可运行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4065字,纯文字阅读大概需要6分钟。
内容图文
![java – @Value无需配置静态PropertySourcesPlaceholderConfigurer即可运行](/upload/InfoBanner/zyjiaocheng/728/c39aa61fbb3e4742866c664ebd853820.jpg)
我想在不使用@Autowiring和Spring Environment类时理解@PropertySource注释的行为.我试图在运行时使用@Value从属性文件中注入值.从我正在阅读的书和在线资源中,需要一个静态bean – PropertySourcesPlaceholderConfigurer,以便它可以工作.但对我来说,@ Value在没有PropertySourcesPlaceholderConfigurer静态bean的情况下也可以工作.有人能指出我正确的方向发生在这里的事情.可能是我遗漏了一些非常基本的东西.我们什么时候需要PropertySourcesPlaceholderConfigurer?什么时候不需要?
以下是我正在尝试的代码 –
package com.nilaysundarkar.demos;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
AppConfig.java –
package com.nilaysundarkar.demos;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource("classpath:/com/nilaysundarkar/demos/app.properties")
public class AppConfig {
// This bean does not make any difference, or does it?
/*@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}*/
@Bean
public Person person(@Value("${person.name}") String name){
Person person = new Person();
person.setName(name);
return person;
}
}
Bootstrap –
package com.nilaysundarkar.demos;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Person person = context.getBean(Person.class);
System.out.println(person.getName());
((AnnotationConfigApplicationContext)context).close();
}
}
属性文件 – app.properties –
person.name=John Doe
pom –
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nilaysundarkar.demos</groupId>
<artifactId>demos-runtime-injections</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
当我运行App.java时 –
解决方法:
在spring boot和spring中,application.properties(以及自弹簧引导后的application.yml)可以放在src / main / resources中,并由spring环境自动选取.这意味着此文件中的任何属性都将加载到您的环境中,并且可以使用@Value进行注入.
您可以使用PropertySourcesPlaceholderConfigurer来注册更多属性源,如foo.properties,[NAME] .properties等,以便spring环境添加它们.
当您使用@PropertySource时,您将另一个propery文件注册到spring环境中,因此您无需使用自定义PropertySourcesPlaceholderConfigurer再次注册它. @PropertySource可以更容易地注册不需要像文件系统中的文件那样的特殊加载的属性文件.
只要使用默认位置(application.properties),就不需要注册此类型的自定义bean.
编辑:
PropertySourcesPlaceholderConfigurer的示例,具有与@PropertySource相同的功能.该示例基于驻留在src / main / resources中的foo.properties文件:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("foo.properties"));
return configurer;
}
内容总结
以上是互联网集市为您收集整理的java – @Value无需配置静态PropertySourcesPlaceholderConfigurer即可运行全部内容,希望文章能够帮你解决java – @Value无需配置静态PropertySourcesPlaceholderConfigurer即可运行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。