spring mvc中的@propertysource
来源:互联网 发布:红蜘蛛教学软件 编辑:程序博客网 时间:2024/05/24 20:06
在spring mvc中,在配置文件中的东西,可以在java代码中通过注解进行读取了:
@PropertySource 在spring 3.1中开始引入
比如有配置文件
config.properties
mongodb.url=1.2.3.4
mongodb.db=hello
则代码中
@PropertySource("classpath:config.properties")public class AppConfigMongoDB { //1.2.3.4 @Value("${mongodb.url}") private String mongodbUrl; //hello @Value("${mongodb.db}") private String defaultDb;@Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); }
则mongodbUrl已经是读取出1.2.3.4的值了,而spring提倡用env去读取值
@Autowired private Environment env;String mongodbUrl = env.getProperty("mongodb.url"); String defaultDb = env.getProperty("mongodb.db");
要注意的是,要使用
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
才能让spring正确解析出${} 中的值
在spring 3.2中,允许支持多个properties了,
@Configuration @PropertySource({ "classpath:config.properties", "classpath:db.properties" //如果是相同的key,则最后一个起作用 }) public class AppConfig { @Autowired Environment env; }
spring 4.1中,支持@PropertySources
@Configuration @PropertySources({ @PropertySource("classpath:config.properties"), @PropertySource("classpath:db.properties") }) public class AppConfig { //... }
在spring 4.2中,
@Configuration @PropertySource("classpath:missing.properties") public class AppConfig { //... }
如果发现missing.properties不存在,则抛出异常
,也可以使用ignoreResourceNotFound=true去忽略
@Configuration
@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
public class AppConfig {
//...
}
0 0
- spring mvc中的@propertysource
- spring mvc中的@propertysource
- spring@PropertySource
- Spring @PropertySource example
- Spring @PropertySource example
- 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注解例子
- 「视频直播技术详解」系列之二:处理
- linux目录树架构示意图
- 下载图片
- JAVA批量下载
- JavaScript笔记:对象
- spring mvc中的@propertysource
- linux下快捷启动关闭tomcat
- Scrapy下xpath基本的使用方法
- linux中的rpm -ivh
- 欢迎使用CSDN-markdown编辑器
- HDOJ 4699 Editor
- EtherCAT主站SOEM源码解析----同步管理器SM配置
- poj2115(欧几里的算法,线性同余方程)
- 万用SharedPreferences