在Spring中,使用PropertyPlaceholderConfigurer和PropertyOverrideConfigurer可以在XML配置文件中加入外部属性文件
但使用这种方式,有一些需要注意的地方
1.首先在主类中,需要使用ClassPathXmlApplicationContext来读取spring配置xml文件
如:
ApplicationContext context = new ClassPathXmlApplicationContext("example4/appcontext.xml");
HelloWorld hw = (HelloWorld)context.getBean("fileHelloWorld");
log.info(hw.getContent());
直接以beanFactory方式,是无法使用PropertyPlaceholderConfigurer或PropertyOverrideConfigurer的
如下方式不行:
Resource resource = new ClassPathResource("example4/appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloWorld hw = (HelloWorld) factory.getBean("fileHelloWorld");
log.info(hw.getContent());
2.PropertyOverrideConfigurer需要考虑bean的名称
如下是正确配置:
appcontext.xml:
<bean name="fileHelloWorld" class="example4.HelloWorld">
<constructor-arg>
<ref bean="fileHello"/>
</constructor-arg>
<property name="statusname">
<value>${fileHelloWorld.statusname}</value>
</property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location" value="classpath:example4/helloworld.properties"/>
</bean>
helloworld.properties:
fileHelloWorld.statusname=this is status Name;
如果少了${fileHelloWorld.statusname}中少了Bean名称fileHelloWorld,会导致错误发生
这应该是种强制某个配置是属性某个Bean
3.PropertyPlaceholderConfigurer的配置不需要考虑Bean的名称,直接配置就可以了
配置方式和PropertyOverrideConfigurer类似,如下配置:
<bean id= "propertyConfigurer"
class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="location" value= "/WEB-INF/jdbc.properties" />
</bean>
<bean id="dataSource" class = "org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" >
<property name="driverClassName" value= "${jdbc.driverClassName}" />
<property name="url" value= "${jdbc.url}" />
<property name="username" value= "${jdbc.username}" />
<property name="password" value= "${jdbc.password}" />
</bean>
在jdbc.properties属性文件中定义属性值:
jdbc.driverClassName= com.mysql.jdbc.Driver
jdbc.url= jdbc:mysql://localhost:3309/sampledb
jdbc.username=root
jdbc.password=1234
分享到:
相关推荐
spring读取properties配置文件的key值。
主要介绍了Spring Boot的properties配置文件读取,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
properties_read_demo.zip
之前的系统用的是Spring框架,国际化还是比较简单的。但是有一些提示是写在js文件里面的,搜了一些,发现一个js国际化的框架jquery.i18n.properties.js,用了一些还是比较方便的。
主要介绍了在SpringBoot下读取自定义properties配置文件的方法,文中涉及到了Spring-boot中读取config配置文件的两种方式,需要的朋友可以参考下
osgi多个bundle之间读取同一配置文件,需要单独添加一个用来读取配置文件的bundle
springboot读取配置文件.rar springboot读取配置文件.rar springboot读取配置文件.rar
本篇文章主要介绍了Spring中配置和读取多个Properties文件的方式方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
1、在maven工程导入spring坐标 2、dao层的内容与结构 接口 实现类 3、server层的内容与结构 4、创建properties文件 5、配置文件样式 标签头代码:
Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素
今天小编就为大家分享一篇关于Spring加载配置和读取多个Properties文件的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
更改“自定义变量”使用“远程配置读取”方式的规则,["dev","test","real"]远程读取,其他则配置文件读取。 更改“全局变量”jsp获取方式。 读取tool-common中的ProjectConstant.onlineEnvSet用于区别线上环境。 ...
主要介绍了Spring Boot中配置文件application.properties使用及spring boot读取application.properties文件的方式,需要的朋友参考下吧
主要介绍了Spring中属性文件properties的读取与使用详解的相关资料,需要的朋友可以参考下
在本文中我们给大家整理了关于Spring Boot 的配置文件 application.properties的相关知识点内容,需要的朋友们参考学习下。
如果不想使用默认的application.properties,而想将属性文件放到jar包外面,怎么做呢?下面小编给大家带来了两种方法解决Spring Boot 把配置文件和日志文件放到jar外部问题,感兴趣的朋友一起看看吧
(可选)要与其他RDBMS一起使用,您需要配置与数据库URL,用户名,密码匹配的项目“ application.properties”文件,并添加所需的Maven依赖项。 技术栈 该项目使用以下技术: REST : 编组: (来自atat的JSON) ...
spring boot 配置文件
背景在 Spring 项目中,你可能需要从 properties 文件中读入配置注入到 bean 中,例如数据库连接信息,redis server的地址端口信息
SpringBoot配置文件类型 SpringBoot配置文件类型和作用: SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用 application.properties或者application.yml...