`

spring mvc中的@propertysource

 
阅读更多
  在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 {
//...
}
 
  
4
0
分享到:
评论

相关推荐

    springMVC rest风格视图解析

    springMVC spring mybatis rest风格架构 根据请求的后缀名 解析成json 或者 xml格式的数据

    spring-boot-reference.pdf

    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抢红包源码-rest-api:基于spring-boot的rest风格的基础框架,整合mysql,oracle,redis,mongo

    java抢红包源码 MobileApp 2016-12-29 升级到spring-boot1.4 neo4j 使用自带的rest 进行图数据库的维护与引擎的调度; 2016-12-27 ...@PropertySource("classpath:application.properties") public class Mo

    thymeleaf-extras-springsecurity-3.0-master.zip

    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 ...

    spring-framework-reference-4.1.2

    Compound property names ....................................................................... 46 Using depends-on .......................................................................................

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    使用Maven POM editor打开项目中的pom.xml文件,选择Dependencies,在Dependencies栏目点击Add进行,首先弹出一个搜索按钮,例如输入spring-web,就会自动搜索关于spring-web相关的jar包,我们选择3.0.5版本的spring...

    spring-framework-reference4.1.4

    Compound property names ....................................................................... 46 Using depends-on .......................................................................................

    达达ASP.NET企业信息管理系统源码 v2.0.zip

    <property name="ConnectionString" value="Data Source=(local);Database=Web;User ID=sa;Password=sa;Trusted_Connection=False" /> </object> (数据库连接字符在这里修改,然后还要重新编译DAO这个项目才...

    Maven权威指南 很精典的学习教程,比ANT更好用

    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知识库

    在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页 通过作业,定时同步两个数据库 SQLSERVER高级注入技巧 利用反射实现ASP.NET控件和...

Global site tag (gtag.js) - Google Analytics