Spring @PropertySource example
来源:互联网 发布:神武2mac版 编辑:程序博客网 时间:2024/05/05 16:35
In Spring, you can use @PropertySource
annotation to externalize your configuration to a properties file. In this tutorial, we will show you how to use @PropertySource
to read a properties file and display the values with @Value
andEnvironment
.
P.S @PropertySource has been available since Spring 3.1
1. @PropertySource and @Value
A classic example, read a properties file and display with ${}
.
mongodb.url=1.2.3.4mongodb.db=hello
package com.mkyong.config;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;//...@Configuration@ComponentScan(basePackages = { "com.mkyong.*" })@PropertySource("classpath:config.properties")public class AppConfigMongoDB {//1.2.3.4@Value("${mongodb.url}")private String mongodbUrl;//hello@Value("${mongodb.db}")private String defaultDb;@Beanpublic MongoTemplate mongoTemplate() throws Exception {MongoClientOptions mongoOptions = new MongoClientOptions.Builder().maxWaitTime(1000 * 60 * 5).build();MongoClient mongo = new MongoClient(mongodbUrl, mongoOptions);MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongo, defaultDb);return new MongoTemplate(mongoDbFactory);}//To resolve ${} in @Value@Beanpublic static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {return new PropertySourcesPlaceholderConfigurer();}}
To resolve ${} in
@Values
, you must register a static PropertySourcesPlaceholderConfigurer
in either XML or annotation configuration file.Spring recommends to use Environment
to get the property values.
package com.mkyong.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;//...@Configuration@ComponentScan(basePackages = { "com.mkyong.*" })@PropertySource("classpath:config.properties")public class AppConfigMongoDB {@Autowiredprivate Environment env;@Beanpublic MongoTemplate mongoTemplate() throws Exception {String mongodbUrl = env.getProperty("mongodb.url");String defaultDb = env.getProperty("mongodb.db");MongoClientOptions mongoOptions = new MongoClientOptions.Builder().maxWaitTime(1000 * 60 * 5).build();MongoClient mongo = new MongoClient(mongodbUrl, mongoOptions);MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongo, defaultDb);return new MongoTemplate(mongoDbFactory);}}
More common examples.
3.1 Example to resolve ${} within @PropertySource
resource locations.
@Configuration@PropertySource("file:${app.home}/app.properties")public class AppConfig {@AutowiredEnvironment env;}
Set a system property during startup.
System.setProperty("app.home", "test");java -jar -Dapp.home="/home/mkyon/test" example.jar
3.2 Include multiple properties files.
@Configuration@PropertySource({"classpath:config.properties","classpath:db.properties" //if same key, this will 'win'})public class AppConfig {@AutowiredEnvironment env;}
If a property key is duplicated, the last declared file will ‘win’ and override.
4. Spring 4 and @PropertySources
Some enhancements on Spring 4.
4.1 Introduces new @PropertySources
to support Java 8 and better way to include multiple properties files.
@Configuration@PropertySources({@PropertySource("classpath:config.properties"),@PropertySource("classpath:db.properties")})public class AppConfig {//...}
4.2 Allow @PropertySource
to ignore the not found properties file.
@Configuration@PropertySource("classpath:missing.properties")public class AppConfig {//...}
If missing.properties
is not found, the system is unable to start and throws FileNotFoundException
Caused by: java.io.FileNotFoundException: class path resource [missiong.properties] cannot be opened because it does not exist
In Spring 4, you can use ignoreResourceNotFound
to ignore the not found properties file
@Configuration@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)public class AppConfig {//...}
@PropertySources({@PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound=true),@PropertySource("classpath:config.properties") })
Done.
References
- Sprong IO – PropertySource
- Spring IO – PropertySources
- Spring IO – Configuration
- Spring @Value default value
- Spring JIRA : SPR-8539
- Spring @PropertySource example
- Spring @PropertySource example
- spring@PropertySource
- spring mvc中的@propertysource
- spring mvc中的@propertysource
- spring @PropertySource classpath
- spring学习7-PropertySource Abstraction
- spring注解 @PropertySource 配置数据源
- @PropertySource
- @PropertySource
- Spring 从文件系统读取配置文件, 使用@PropertySource
- spring 源码研究---core包-- PropertySource Environment
- Spring 使用@PropertySource读取properties文件
- Spring的@PropertySource和@Value注解例子
- Spring 4 @PropertySource和@Value注解示例
- Spring的@PropertySource和@Value注解例子
- Spring 4 @PropertySource和@Value注解示例
- Spring的@PropertySource和@Value注解例子
- 对机器学习的一点理解
- 【笔记】Discovering and Exploiting Deterministic Label Relationships in Multi-Label Learning
- Android双击退出应用程序
- 关于xutils3.0框架的封装以及抽取,方便使用
- Spark 1.5.2 on yarn升级问题总结
- Spring @PropertySource example
- android学习之SoundPool
- PHPMailer使用
- iOS 微信第三方登录的简单实现
- 初探Hibernate
- VR
- KAFKA学习总结
- Android动画实战-实现下拉式窗帘动画和上拉式抽屉动画
- androidapk安装过程详解