spring 读取properties文件
spring 版本:4.3.2.RELEASE
注解方式
使用PropertySource
package com.stub.conf; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; /** * Created by 黄威 on 9/18/16.<br > */ @Configuration @PropertySource({"classpath:/com/app.properties","classpath:/${appPath}/app2.properties"}) //@PropertySources({ // @PropertySource("classpath:/com/app.properties"), // @PropertySource("classpath:/${appPath}/app2.properties") //}) public class EnvMyBean { @Autowired Environment env; public String getProperty(String key){ return env.getProperty(key); } }
说明:
变量${appPath}是从第一个配置文件(classpath:/com/app.properties)中读取的.
注意:低版本(例如3.2.3.RELEASE)的spring 是无法读取${appPath}的
如何使用呢?
在控制器中注入EnvMyBean即可.
profile
适用场景:
(a)测试环境,使用@ActiveProfiles 指定profile
@ActiveProfiles(profiles = "dev") public class DevConfigTest { //code here }
(b)不同的环境使用不同的配置
通过配置web.xml:
<context-param> <param-name>spring.profiles.active</param-name> <param-value>DOUBLEUPMINT</param-value> </context-param>
(c)通过程序指定
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("dev"); ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class); ctx.refresh();
也可以重载全局监听器:
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletContextEvent; /** * Created by 黄威 on 9/18/16.<br > */ public class ApplicationListener extends ContextLoaderListener { public ApplicationListener(WebApplicationContext context) { super(context); ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment(); System.out.println("ApplicationListener contructor"); System.out.println(environment); } public ApplicationListener() { super(); } @Override public void contextInitialized(ServletContextEvent event) { super.contextInitialized(event); WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); ConfigurableEnvironment environment = (ConfigurableEnvironment) ctx.getEnvironment(); System.out.println(environment); environment.setActiveProfiles("production"); } }
使用PropertyPlaceholderConfigurer 需要在xml配置
<bean id="PropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties/app.properties</value> </list> </property> </bean>
参考:http://javabeat.net/profile-annotation-spring-4/
相关推荐
Spring 读取properties文件key+value方式.rar
spring读取properties.
spring读取properties配置文件的key值。
主要介绍了spring无法读取properties文件数据问题详解,需要的朋友可以参考下
主要介绍了Spring用代码来读取properties文件实例解析,具有一定借鉴价值,需要的朋友可以参考下
NULL 博文链接:https://nannan408.iteye.com/blog/2256177
主要介绍了Spring Boot的properties配置文件读取,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Spring中属性文件properties的读取与使用详解的相关资料,需要的朋友可以参考下
1、在maven工程导入spring坐标 2、dao层的内容与结构 接口 实现类 3、server层的内容与结构 4、创建properties文件 5、配置文件样式 标签头代码:
本篇文章主要介绍了Spring中配置和读取多个Properties文件的方式方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
之前的系统用的是Spring框架,国际化还是比较简单的。但是有一些提示是写在js文件里面的,搜了一些,发现一个js国际化的框架jquery.i18n.properties.js,用了一些还是比较方便的。
Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素
今天小编就为大家分享一篇关于Spring加载配置和读取多个Properties文件的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
properties_read_demo.zip
主要介绍了在SpringBoot下读取自定义properties配置文件的方法,文中涉及到了Spring-boot中读取config配置文件的两种方式,需要的朋友可以参考下
JSP 中Spring的Resource类读写中文Properties 摘要: Spring对Properties的读取进行了完善而全面的封装,对于写则仍需配合FileOutputStream进行。 package com.oolong.common.util; import org.springframework....
在本文中我们给大家整理了关于Spring Boot 的配置文件 application.properties的相关知识点内容,需要的朋友们参考学习下。
主要介绍了详解五种方式让你在java中读取properties文件内容不再是难题 ,非常具有实用价值,需要的朋友可以参考下。
主要介绍了Spring Boot中配置文件application.properties使用及spring boot读取application.properties文件的方式,需要的朋友参考下吧
springboot读取配置文件.rar springboot读取配置文件.rar springboot读取配置文件.rar