在spring mvc中,在配置文件中的东西,可以在java代码中通过注解进行读取了:

@PropertySource  在spring 3.1中开始引入

比如有配置文件

config.properties

mongodb.url=1.2.3.4

mongodb.db=hello

则代码中

  1. @PropertySource("classpath:config.properties")
  2. public class AppConfigMongoDB {
  3.  
  4. //1.2.3.4
  5. @Value("${mongodb.url}")
  6. private String mongodbUrl;
  7.  
  8. //hello
  9. @Value("${mongodb.db}")
  10. private String defaultDb;
  11.  
  12. @Bean
  13. public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
  14. return new PropertySourcesPlaceholderConfigurer();
  15. }

则mongodbUrl已经是读取出1.2.3.4的值了,而spring提倡用env去读取值

  1. @Autowired
  2. private Environment env;
  3.  
  4. String mongodbUrl = env.getProperty("mongodb.url");
  5. String defaultDb = env.getProperty("mongodb.db");

要注意的是,要使用

@Bean

public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {

return new PropertySourcesPlaceholderConfigurer();

}

才能让spring正确解析出${} 中的值

在spring 3.2中,允许支持多个properties了,

  1. @Configuration
  2. @PropertySource({
  3. "classpath:config.properties",
  4. "classpath:db.properties" //如果是相同的key,则最后一个起作用
  5. })
  6. public class AppConfig {
  7. @Autowired
  8. Environment env;
  9. }

spring 4.1中,支持@PropertySources

  1. @Configuration
  2. @PropertySources({
  3. @PropertySource("classpath:config.properties"),
  4. @PropertySource("classpath:db.properties")
  5. })
  6. public class AppConfig {
  7. //...
  8. }

在spring 4.2中,

  1. @Configuration
  2. @PropertySource("classpath:missing.properties")
  3. public class AppConfig {
  4. //...
  5. }

如果发现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. 【BZOJ 3620】 似乎在梦中见过的样子

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3620 [算法] KMP [代码] #include<bits/stdc++.h ...

  2. B1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 数学

    今天净做水题了,这个题还不到十五分钟就搞定了,思路特别简单,就是直接按照线性求因子个数的思路就行了. 题干: Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜 ...

  3. Human Gene Functions(dp)

    http://poj.org/problem?id=1080 #include <stdio.h> #include <stdlib.h> #include <strin ...

  4. Eqs(枚举+ hash)

    http://poj.org/problem?id=1840 题意:给出系数a1,a2,a3,a4,a5,求满足方程的解有多少组. 思路:有a1x13+ a2x23+ a3x33+ a4x43+ a5 ...

  5. 试用php的ping命令

    使用PHP自动PING IP,校检网络连接是否正常! <?php $server = 'ping kalvin.cn -n 1'; $last_line = exec($server, $arr ...

  6. 日期数据类型为Date ,前台传递喂String的后台处理

    方法一: 在实体类里面将set方法里面将数据类型转为Date public void setBirth(String birth) { SimpleDateFormat sdf = new Simpl ...

  7. My97DatePicker 动态设置有效/无效日期

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  8. angualr2之路由与导航(一)

    1. 唯一根模块式组件导航 在唯一根模块式组件导航中,全局只有一个模块,即根模块. 1.1 根模块 在根模块中,需要导入所有组件及其对应的服务,并将组件加入声明,将其对应的服务加入提供商. // ap ...

  9. mysql Workbench新建数据库连接时提示SSl not enabled的问题

    在使用workbench时会弹出以下的对话框,不过这不影响使用,点击ok之后对话框关闭,接着直接点击close就行了,之后会显示已经连接上了数据库.具体ssl的方面还没研究,等研究了再贴

  10. dubbo之启动时检查

    启动时检查 Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认 check="true".所以可以通过 ...