繁体   English   中英

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

[英]Spring MVC Rest App - Best Practice to Loading Properties

已分配来重新评估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文件,该声明是什么?

  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来输入安全性(如果没有其他要求)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM