在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 {

//...

}

spring mvc中的@propertysource的更多相关文章

  1. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  2. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  3. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

  4. Spring mvc中@RequestMapping 6个基本用法小结(转载)

    小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...

  5. Spring MVC中处理静态资源的多种方法

    处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中 ...

  6. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  7. spring mvc中的文件上传

    使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...

  8. spring mvc中的valid

    当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...

  9. spring mvc中的@PathVariable(转)

    鸣谢:http://jackyrong.iteye.com/blog/2059307 ------------------------------------------------ spring m ...

随机推荐

  1. .Net-ASP.NET Web API:目录

    ylbtech-.Net-ASP.NET Web API:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. https://www.asp.net/we ...

  2. Java-java-com-util-common-service:TreeService.java

    ylbtech-Java-java-com-util-common-service:TreeService.java 1.返回顶部 1. package com.shineyoo.manager.ut ...

  3. 迭代,IDA*

    1.codevs1288 题意:对于一个分数a/b(a!=1),将它表示为1/x + 1/y + 1/z ……的形式,x,y,z……互不相同 多解取加数少的,加数相同时,取最小的分数最大的. 思路:经 ...

  4. 5.12redis

    Window配置Redis环境和简单使用 一.关于Redis Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符串.哈希表.列表.集合.有序 ...

  5. 软件架构自学笔记——常见的软件架构(https://jiajunhuang.com/articles/2018_09_16-common_software_archtecture_pattern.md.html)

    分层模式 这种模式主要是将设计分层,每一层为其上层提供服务.例如:web开发中我们常常将某些常用的RESTful接口抽象出一个service层. 客户端-服务端模式 客户端和服务端分离,从而解耦.通过 ...

  6. Android,加载离线Android API文档缓慢问题!

    解决方法:在host文件末添加如下信息! 0.0.0.0 www.googleapis.com 0.0.0.0 www.google.com 0.0.0.0 www.google-analytics. ...

  7. 【MySQL】通信协议

    1.TCP/IP(Transmission Control Protocol/Internet Protocol) 该通信协议套件用于连接 Internet 上的主机.在 Linux 操作系统中,TC ...

  8. Caffe: Vs13添加CUDA支持

    1.  右键工程 点击:Building Dependency 右击:Build Customizations 点击选项:CUDA 7.5 2.添加C++依赖: cudart.lib kernel32 ...

  9. Arduino 控制超声波测距模块

    一.实物图 二.例子代码 用到数字2 和3 引脚,还有两个就是vcc GND两个阴脚,用模块连线比较简单

  10. node jsonwebtoken

     jsonwebtoken是node版本的JWT(JSON Web Tokens)的实现.1.什么是JWT?Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于J ...