【问题标题】:Spring MVC Rest App - Best Practice to Loading PropertiesSpring MVC Rest App - 加载属性的最佳实践
【发布时间】:2017-07-09 08:57:47
【问题描述】:

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

WEB-INF/mvc-dispatcher-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/main/resources/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/main/resources/prop.properties:

banner = images/banner.png

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

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

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

src/main/resources/config.properties

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

问题:

  1. 重组其中一些配置文件的最佳方法是什么?

  2. 我应该在哪里声明这个新的 config.properties 文件,声明是什么?

【问题讨论】:

  • 在 Spring Boot 中,您只需在 application.properties 文件中添加其他属性。然后使用@Value 注入值。像 @Value("${com.test.config1}") 私有字符串配置;您可以根据如何命名包来确定配置的范围,以避免配置名称冲突。
  • 抱歉,这不是 Spring Boot - 它的 Spring 4 MVC...

标签: java spring spring-mvc


【解决方案1】:
  1. 将属性文件放入src/main/resources

2.

@Configuration
@PropertySource("classpath:config.properties")
public class PropertiesWithJavaConfig {

   @Bean
   public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
   }
}

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

PropertyPlaceholderConfigurer 多年来一直被PropertySourcesPlaceholderConfigurer 弃用。我希望您放弃 XML 并转而使用 JavaConfig,以确保类型安全。

【讨论】:

    猜你喜欢
    • 2015-09-11
    • 2017-11-13
    • 2011-10-27
    • 2015-04-19
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多