【问题标题】:How to share propertyplaceholder for 2 context如何共享 2 个上下文的属性占位符
【发布时间】:2013-10-02 23:21:36
【问题描述】:

我在 Spring 3.2.4 上有 Spring MVC Web 应用程序。 我有 2 个上下文。 1. mvc-dispatcher-servlet.xml 看起来像这样:

<context:annotation-config/>
    <context:component-scan base-package="com.ja.dom"/>

    <!-- Tiles 3 config -->

    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <!--Don't add suffix or prefix like you do with .jsp files-->
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
    </bean>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" >
        <property name="definitions">
            <value>/WEB-INF/tiles.xml</value>
        </property>
    </bean>

和 2. 像这样的 root-context.xml:

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations" value="classpath:prop.properties"/>
 </bean>

 <!-- Mail Sender Bean -->

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
     <property name="host" value="smtp.gmail.com" />
     <property name="port" value="587" />
     <property name="username" value="${email.name}" />
     <property name="password" value="${email.password}" />
     <property name="defaultEncoding" value="UTF-8"/>

     <property name="javaMailProperties">
         <props>
             <prop key="mail.smtp.auth">true</prop>
             <prop key="mail.smtp.starttls.enable">true</prop>
             <prop key="mail.debug">true</prop>
         </props>
     </property>
 </bean>

当我运行应用程序时,我看到只有 mailSender bean 具有属性文件中的参数。 我的其他 bean 等 @Service .. 不被注入。怎么了? 如何将我的 PropertyPlaceholderConfigurer 共享到 mvc-dispatcher-servlet 上下文?

我像这样在 Service 上注入属性:

@Value("${reCaptcha.private.key}") 私有字符串 reCaptchaPrivateKey;

和我的 web.xml (更新)

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <display-name>Spring MVC Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--<init-param>-->
            <!--<param-name>contextConfigLocation</param-name>-->
            <!--<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>-->
        <!--</init-param>-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>




    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    我认为问题出在

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:prop.properties"/>
    </bean>
    

    相反,您应该使用PropertySourcesPlaceholderConfigurer

    <context:property-placeholder location="classpath:prop.properties" />
    

    哪个

    从 Spring 3.1 开始,XML 将不再 不再注册旧的 PropertyPlaceholderConfigurer 但新的 介绍了 PropertySourcesPlaceholderConfigurer。这个替换 类的创建更加灵活,并且可以更好地与 新引入的 Environment 和 PropertySource 机制;这应该 被视为 3.1 应用程序的标准。

    【讨论】:

    • 2 每个上下文文件的属性占位符 bean ?对吗?
    • @jToM 每个人一个,是的。
    • 这是春季最佳实践?
    • @jToM 不一定。您可以将所有属性保留在同一个文件中,并让每个 PropertyPlaceholderConfigurer 从同一个位置读取。在我看来,这对你的情况会很好。
    • @JtoM 我真的错了。给我几分钟,但请去掉你的复选标记。
    猜你喜欢
    • 2013-12-06
    • 1970-01-01
    • 2012-08-03
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多