Spring 中加载资源文件

来源:互联网 发布:网络电视pc客户端 编辑:程序博客网 时间:2024/04/30 19:56

 在Spring 中可以使用以下两个类加载资源文件:

org.springframework.context.support.ResourceBundleMessageSource

org.springframework.context.support.ReloadableResourceBundleMessageSource

后者可以不重起服务器的情况下,读取资源文件,所以可以随时更改资源文件。

以下为在其配置:

  1. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  2.         <property name="basename">
  3.             <value>ApplicationResources</value>
  4.         </property>
  5. </bean>
  1. <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  2.         <property name="basename">
  3.             <value>ApplicationResources</value>
  4.         </property>
  5. </bean>

当上下文件配置文件被读取后,以在配置文件中指定的名字为基名的资源文件全部会被读入,并置于上下文中,这样以后就可以在上下文中读取,在页面上也可以使用Spring标签取得。

----------------------------------

在SPRING中提供了一个专门加载属性文件的类,我们只要给它指定属性文件所在的路径即可。这样我们就可以把一些经常变化的配置放入特定的属性文件中。下面为该类在SPRING中的配置:

  1.     <bean id="propertyConfigurer"       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2.         <property name="location">
  3.             <value>classpath:com/starxing/test/database.properties</value>
  4.         </property>
  5. <!-- 使用locations属性定义多个配置文件
  6.        <property name="locations">
  7.             <list>
  8.                 <value>classpath:config/mail.properties</value>
  9.                 <value>classpath:config/database.properties</value>
  10.             </list>
  11. </property>
  12. -->
  13. </bean>
  1. <bean id="dataSource"
  2.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  3.         <property name="url">
  4.             <value>${database.url}</value>
  5.         </property>
  6.         <property name="driverClassName">
  7.             <value>${database.driver}</value>
  8.         </property>
  9.         <property name="username">
  10.             <value>${database.user}</value>
  11.         </property>
  12.         <property name="password">
  13.             <value>${database.password}</value>
  14.         </property>
  15. </bean>

database.properties

database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost/test
database.user=postgres
database.password=123456
propertyConfigurer加载database.properties文件,为创建dataSource提供参数。



原创粉丝点击