- 浏览: 7852013 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (2425)
- 软件工程 (75)
- JAVA相关 (662)
- ajax/web相关 (351)
- 数据库相关/oracle (218)
- PHP (147)
- UNIX/LINUX/FREEBSD/solaris (118)
- 音乐探讨 (1)
- 闲话 (11)
- 网络安全等 (21)
- .NET (153)
- ROR和GOG (10)
- [网站分类]4.其他技术区 (181)
- 算法等 (7)
- [随笔分类]SOA (8)
- 收藏区 (71)
- 金融证券 (4)
- [网站分类]5.企业信息化 (3)
- c&c++学习 (1)
- 读书区 (11)
- 其它 (10)
- 收藏夹 (1)
- 设计模式 (1)
- FLEX (14)
- Android (98)
- 软件工程心理学系列 (4)
- HTML5 (6)
- C/C++ (0)
- 数据结构 (0)
- 书评 (3)
- python (17)
- NOSQL (10)
- MYSQL (85)
- java之各类测试 (18)
- nodejs (1)
- JAVA (1)
- neo4j (3)
- VUE (4)
- docker相关 (1)
最新评论
-
xiaobadi:
jacky~~~~~~~~~
推荐两个不错的mybatis GUI生成工具 -
masuweng:
(转)JAVA获得机器码的实现 -
albert0707:
有些扩展名为null
java 7中可以判断文件的contenttype了 -
albert0707:
非常感谢!!!!!!!!!
java 7中可以判断文件的contenttype了 -
zhangle:
https://zhuban.me竹板共享 - 高效便捷的文档 ...
一个不错的网络白板工具
在spring mvc中,在配置文件中的东西,可以在java代码中通过注解进行读取了:
@PropertySource 在spring 3.1中开始引入
比如有配置文件
config.properties
mongodb.url=1.2.3.4
mongodb.db=hello
则代码中
则mongodbUrl已经是读取出1.2.3.4的值了,而spring提倡用env去读取值
要注意的是,要使用
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
才能让spring正确解析出${} 中的值
在spring 3.2中,允许支持多个properties了,
spring 4.1中,支持@PropertySources
在spring 4.2中,
如果发现missing.properties不存在,则抛出异常
,也可以使用ignoreResourceNotFound=true去忽略
@Configuration
@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
public class AppConfig {
//...
}
@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 {
//...
}
发表评论
-
复习:强迫线程顺序执行方式
2019-01-03 23:42 1477方法1: 三个线程,t1,t2,t3,如果一定要按顺序执行, ... -
(转)不错的前后端处理异常的方法
2019-01-02 23:16 1965前言 在 Web 开发中, 我们经常会需要处理各种异常, 这是 ... -
info q的极客时间大咖说等资料下载
2018-08-15 08:40 3411info q的极客时间大咖说等资料下载,还有不少思维导图 链 ... -
CXF 客户端超时时间设置(非Spring配置方式)
2018-07-03 22:38 2183import org.apache.cxf.endpoint. ... -
(转)synchronized关键字画像:正确打开方式
2018-06-14 09:25 446https://mp.weixin.qq.com/s/b3Sx ... -
CountDownLatch的例子
2018-06-13 14:10 628public class StatsDemo { ... -
两道面试题,带你解析Java类加载机制
2018-06-12 16:29 551https://mp.weixin.qq.com/s/YTa0 ... -
Spring中获取request的几种方法,及其线程安全性分析
2018-06-11 09:03 621https://mp.weixin.qq.com/s/KeFJ ... -
内部类小结
2018-06-06 10:25 394https://mp.weixin.qq.com/s/hErv ... -
JVM虚拟机小结1
2018-06-04 20:43 4611 jps -l //列出详细的类名和进程ID 2)jps ... -
windows下自带命令行工具查看CPU资源情况等
2018-06-04 12:53 3038微软提供了不少命令行 ... -
(收藏)深入分析Java的序列化与反序列化
2018-05-30 15:21 553https://mp.weixin.qq.com/s/T2Bn ... -
apache common包中的序列化工具
2018-05-30 09:10 1773什么是序列化 我们的 ... -
JAVA8 JVM的变化: 元空间(Metaspace)
2018-05-24 22:30 907本文将会分享至今为至我收集的关于永久代(Permanent G ... -
(转)服务器性能指标(一)——负载(Load)分析及问题排查
2018-05-21 21:03 1262原创: Hollis Hollis 负载 ... -
(转)对象复用
2018-05-20 15:27 804public class Student { priv ... -
mapreduce中入门中要注意的几点
2018-05-06 08:59 617在 mapreduce中,比如有如下的词: I love b ... -
HDFS的基本操作
2018-05-02 21:47 878-mkdir 在HDFS创建目录 ... -
一个不错的开源工具类,专门用来解析日志头部的,好用
2018-05-02 20:00 705一个不错的开源工具类,专门用来解析日志头部的,好用。 http ... -
介绍个不错的RESTFUL MOCK的工具wiremock
2018-04-27 21:02 1853介绍个不错的RESTFUL MOCK的工具wiremock,地 ...
相关推荐
springMVC spring mybatis rest风格架构 根据请求的后缀名 解析成json 或者 xml格式的数据
Auto-configured Spring REST Docs Tests with Mock MVC Auto-configured Spring REST Docs Tests with REST Assured 43.3.20. User Configuration and Slicing 43.3.21. Using Spock to Test Spring Boot ...
java抢红包源码 MobileApp 2016-12-29 升级到spring-boot1.4 neo4j 使用自带的rest 进行图数据库的维护与引擎的调度; 2016-12-27 ...@PropertySource("classpath:application.properties") public class Mo
In order to use the thymeleaf-extras-springsecurity3 or thymeleaf-extras-springsecurity4 modules in our Spring MVC application, we will first need to configure our application in the usual way for ...
Compound property names ....................................................................... 46 Using depends-on .......................................................................................
使用Maven POM editor打开项目中的pom.xml文件,选择Dependencies,在Dependencies栏目点击Add进行,首先弹出一个搜索按钮,例如输入spring-web,就会自动搜索关于spring-web相关的jar包,我们选择3.0.5版本的spring...
Compound property names ....................................................................... 46 Using depends-on .......................................................................................
<property name="ConnectionString" value="Data Source=(local);Database=Web;User ID=sa;Password=sa;Trusted_Connection=False" /> </object> (数据库连接字符在这里修改,然后还要重新编译DAO这个项目才...
Property References in Assembly Descriptors 12.4.2. Required Assembly Information 12.5. Controlling the Contents of an Assembly 12.5.1. Files Section 12.5.2. FileSets Section 12.5.3. Default ...
在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页 通过作业,定时同步两个数据库 SQLSERVER高级注入技巧 利用反射实现ASP.NET控件和...