Spring 中加载资源文件
2012-04-07 11:06
295 查看
在Spring 中可以使用以下两个类加载资源文件:
org.springframework.context.support.ResourceBundleMessageSource
和
org.springframework.context.support.ReloadableResourceBundleMessageSource
后者可以不重起服务器的情况下,读取资源文件,所以可以随时更改资源文件。
以下为在其配置:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>ApplicationResources</value>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename">
<value>ApplicationResources</value>
</property>
</bean>
当上下文件配置文件被读取后,以在配置文件中指定的名字为基名的资源文件全部会被读入,并置于上下文中,这样以后就可以在上下文中读取,在页面上也可以使用Spring标签取得。
----------------------------------
在SPRING中提供了一个专门加载属性文件的类,我们只要给它指定属性文件所在的路径即可。这样我们就可以把一些经常变化的配置放入特定的属性文件中。下面为该类在SPRING中的配置:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/starxing/test/database.properties</value>
</property>
<!-- 使用locations属性定义多个配置文件
<property name="locations">
<list>
<value>classpath:config/mail.properties</value>
<value>classpath:config/database.properties</value>
</list>
</property>
-->
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${database.url}</value>
</property>
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="username">
<value>${database.user}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
</bean>
database.properties
database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost/test
database.user=postgres
database.password=123456
propertyConfigurer加载database.properties文件,为创建dataSource提供参数。
org.springframework.context.support.ResourceBundleMessageSource
和
org.springframework.context.support.ReloadableResourceBundleMessageSource
后者可以不重起服务器的情况下,读取资源文件,所以可以随时更改资源文件。
以下为在其配置:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>ApplicationResources</value>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename">
<value>ApplicationResources</value>
</property>
</bean>
当上下文件配置文件被读取后,以在配置文件中指定的名字为基名的资源文件全部会被读入,并置于上下文中,这样以后就可以在上下文中读取,在页面上也可以使用Spring标签取得。
----------------------------------
在SPRING中提供了一个专门加载属性文件的类,我们只要给它指定属性文件所在的路径即可。这样我们就可以把一些经常变化的配置放入特定的属性文件中。下面为该类在SPRING中的配置:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/starxing/test/database.properties</value>
</property>
<!-- 使用locations属性定义多个配置文件
<property name="locations">
<list>
<value>classpath:config/mail.properties</value>
<value>classpath:config/database.properties</value>
</list>
</property>
-->
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${database.url}</value>
</property>
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="username">
<value>${database.user}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
</bean>
database.properties
database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost/test
database.user=postgres
database.password=123456
propertyConfigurer加载database.properties文件,为创建dataSource提供参数。
相关文章推荐
- spring-boot 加载本地静态资源文件路径配置
- spring-mvc加载静态资源文件
- spring boot 加载本地静态资源文件路径配置
- Spring集成多个资源文件加载问题
- spring 加载多个资源文件
- Spring 中加载资源文件
- spring 加载资源文件
- Spring中@ImportResource和@Value加载资源文件
- spring和java国际化资源文件加载顺序
- spring-boot 加载本地静态资源文件路径配置
- Spring boot 国际化自动加载资源文件去除默认国际化文件
- 解决 IntelliJ IDEA 无法热加载 Spring Boot 模板文件和静态资源文件
- Spring 中加载资源文件
- Spring之ClassPathResource加载资源文件
- spring boot实战(第六篇)加载application资源文件源码分析
- spring同时集成遇到多个properties资源文件加载的问题
- Spring资源文件的加载
- spring boot实战(第六篇)加载application资源文件源码分析
- 解决SpringBoot整合shiro之后无法加载静态资源文件问题
- spring 加载资源文件的两个类ResourceBundleMessageSource和ReloadableResourceBundleMessageSource