spring4.0 @PropertySource读取配置文件
来源:互联网 发布:网络大电影方案 编辑:程序博客网 时间:2024/05/18 05:38
@Configurable
@ComponentScan(basePackages = "com.9leg.java.spring")
@PropertySource(value = "classpath:spring/config.properties")
public class AppConfigTest {
@Bean
public PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
通过@PropertySource注解将properties配置文件中的值存储到Spring的 Environment中,Environment接口提供方法去读取配置文件中的值,参数是properties文件中定义的key值。上面是读取一个配置文件,如果你想要读取多个配置文件,请看下面代码片段:
@PropertySource(value = {"classpath:spring/config.properties","classpath:spring/news.properties"})
在Spring 4版本中,Spring提供了一个新的注解——@PropertySources,从名字就可以猜测到它是为多配置文件而准备的。
@PropertySources({
@PropertySource("classpath:config.properties"),
@PropertySource("classpath:db.properties")
})
public class AppConfig {
//something
}
另外在Spring 4版本中,@PropertySource允许忽略不存在的配置文件。先看下面的代码片段:
@Configuration
@PropertySource("classpath:missing.properties")
public class AppConfig {
//something
}
如果missing.properties不存在或找不到,系统则会抛出异常FileNotFoundException。
Caused by: java.io.FileNotFoundException:
class path resource [missiong.properties] cannot be opened because it does not exist
幸好Spring 4为我们提供了ignoreResourceNotFound属性来忽略找不到的文件
@Configuration
@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
public class AppConfig {
}
@PropertySources({
@PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound=true),
@PropertySource("classpath:config.properties")
})
以下是测试例子 读取kapacha 属性:
package com.trunko.oa.config;import java.util.Properties;import org.springframework.beans.factory.annotation.Autowired;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;import org.springframework.core.env.Environment;import com.google.code.kaptcha.impl.DefaultKaptcha;import com.google.code.kaptcha.util.Config;@Configuration@PropertySource("classpath:kapatcha.properties")public class SpringCofig {/**方式一:@Value("${isBorder}")private String isBorder;@Value("${borderColor}")private String borderColor;@Value("${fontColor}")private String fontColor;@Value("${imgWidth}")private String imgWidth;@Value("${imgHeight}")private String imgHeight;@Value("${fontSize}")private String fontSize;@Value("${sessionKey}")private String sessionKey;@Value("${codeLength}")private String codeLength;@Value("${fontName}") private String fontName; @Bean //要想使用@Value 用${}占位符注入属性,这个bean是必须的,这个就是占位bean,另一种方式是不用value直接用Envirment变量直接getProperty('key') public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); }@Beanpublic DefaultKaptcha captchaProducer(){System.out.println(isBorder);Properties ps= new Properties();ps.setProperty("kaptcha.border", isBorder);ps.setProperty("kaptcha.border.color", borderColor);ps.setProperty("kaptcha.textproducer.font.color", fontColor);ps.setProperty("kaptcha.image.width", imgWidth);ps.setProperty("kaptcha.image.height", imgHeight);ps.setProperty("kaptcha.textproducer.font.size", fontSize);ps.setProperty("kaptcha.session.key", sessionKey);ps.setProperty("kaptcha.textproducer.char.length", codeLength);ps.setProperty("kaptcha.textproducer.font.names", fontName);Config config = new Config(ps);DefaultKaptcha df= new DefaultKaptcha();df.setConfig(config);return df;}方式一结束**///方式二:用Envirment方式@Autowiredprivate Environment env;@Beanpublic DefaultKaptcha captchaProducer(){Properties ps= new Properties();ps.setProperty("kaptcha.border", env.getProperty("isBorder"));ps.setProperty("kaptcha.border.color",env.getProperty("borderColor"));ps.setProperty("kaptcha.textproducer.font.color", env.getProperty("fontColor"));ps.setProperty("kaptcha.image.width", env.getProperty("imgWidth"));ps.setProperty("kaptcha.image.height",env.getProperty("imgHeight"));ps.setProperty("kaptcha.textproducer.font.size", env.getProperty("fontSize"));ps.setProperty("kaptcha.session.key", env.getProperty("sessionKey"));ps.setProperty("kaptcha.textproducer.char.length", env.getProperty("codeLength"));ps.setProperty("kaptcha.textproducer.font.names",env.getProperty("fontName"));Config config = new Config(ps);DefaultKaptcha df= new DefaultKaptcha();df.setConfig(config);return df;}//@Bean//public DefaultKaptcha captchaProducer(){//Properties ps= new Properties();//ps.setProperty("kaptcha.border", "yes");//ps.setProperty("kaptcha.border.color", "105,179,90");//ps.setProperty("kaptcha.textproducer.font.color", "blue");//ps.setProperty("kaptcha.image.width", "125");//ps.setProperty("kaptcha.image.height", "45");//ps.setProperty("kaptcha.textproducer.font.size", "45");//ps.setProperty("kaptcha.session.key", "code");//ps.setProperty("kaptcha.textproducer.char.length", "4");//ps.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");//Config config = new Config(ps);//DefaultKaptcha df= new DefaultKaptcha();//df.setConfig(config);//return df;////}}配置文件:
isBorder=yesborderColor=105,179,90fontColor=blueimgWidth=125imgHeight=45fontSize=45sessionKey=codecodeLength=4fontName=\u5B8B\u4F53,\u6977\u4F53,\u5FAE\u8F6F\u96C5\u9ED1
- spring4.0 @PropertySource读取配置文件
- Spring 从文件系统读取配置文件, 使用@PropertySource
- Spring boot 通过@ConfigurationProperties和@PropertySource读取自定义配置文件信息
- 【Spring4揭秘 基础2】PropertySource和Enviroment
- @PropertySource
- @PropertySource
- Spring 使用@PropertySource读取properties文件
- Spring 使用@PropertySource+Emvironment读取properties文件
- Spring4+hibernate4+struts2配置文件
- spring4的配置文件模板
- spring4配置文件详解
- spring4配置文件详解
- spring4配置文件详解
- spring4 + redis 零配置文件
- 新版Spring4.0中加载配置文件的类发生改变
- spring4.2整合hibernate5配置文件
- Spring4+MyBatis3整合配置文件详解
- Spring4+Mybatis不能读取 properties
- 查找序列最宽尖峰——含代码
- 2017春招去哪儿笔试
- socket通信
- hdu 2604 矩阵快速幂
- 堆排序
- spring4.0 @PropertySource读取配置文件
- linux ping不通的解决办法
- Java Annotation的RetentionPolicy介绍
- JVM笔记
- eclipse的Tomcat插件配置
- ARM MMU工作原理剖析
- CentOS7安装mysql数据库
- 基于UDP信息传输(数据类型包装转换)
- C++ 实现 atoi