Spring MVC Rest App - 加载属性的最佳实践

时间:2017-07-09 08:57:48

标签: java spring spring-mvc

已被指派重新评估以前的开发人员所在的Spring 4 MVC Rest应用程序 将配置属性的加载放在以下位置:

WEB-INF / MVC-调度-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

    <import resource="classpath:mydatabase.xml" />

    <context:component-scan base-package="com.myapp.rest, com.myapp.config" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <util:properties id="props" location="classpath:prop.properties" />
</beans>

的src /主/资源/ mydatabase.xml:

<bean id="propertyPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="location" value="file:/opt/database.properties">
</property>

<bean id="mydatabase" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>${db.driver}</value></property>
    <property name="url"><value>${db.url}</value></property>
    <property name="username"><value>${db.username}</value></property>
    <property name="password"><value>${db.password}</value></property>
    <property name="maxIdle" value="10" />
    <property name="maxActive" value="50" />
    <property name="maxWait" value="100" />
    <property name="defaultAutoCommit" value="false" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="1" />
    <property name="minIdle" value="0"></property>
    <property name="timeBetweenEvictionRunsMillis" value="1000"></property>
    <property name="minEvictableIdleTimeMillis" value="1000"></property>
</bean>

的src /主/资源/ prop.properties:

banner = images/banner.png

在代码中,我一直看到人们使用以下方式插入横幅文件位置:

private @Value("#{props[banner]}") String banner;

我的目标是添加一个新的属性文件:

src/main/resources/config.properties

所以,我可以使用@Value注释......

问题(S):

  1. 重新组织部分配置文件的 BEST 方式是什么?

  2. 我将在哪里声明这个新的config.properties文件以及声明是什么?

1 个答案:

答案 0 :(得分:0)

  1. 将属性文件放在src/main/resources
  2. 2

    @Configuration
    @PropertySource("classpath:config.properties")
    public class PropertiesWithJavaConfig {
    
       @Bean
       public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
          return new PropertySourcesPlaceholderConfigurer();
       }
    }
    

    如果您要加载多个文件,请使用PropertySources

    多年来,

    PropertyPlaceholderConfigurer已被弃用,有利于PropertySourcesPlaceholderConfigurer。我希望您放弃XML并转移到JavaConfig,如果不是其他任何东西,请输入安全性。