java - 如何共享 2 上下文的属性占位符

标签 java spring spring-mvc

我在 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 .. 未被注入(inject)。怎么了? 如何将我的 PropertyPlaceholderConfigurer 共享到 mvc-dispatcher-servlet 上下文?

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

@Value("${reCaptcha.private.key}") private String 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>

最佳答案

我认为问题在于

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

相反,您应该使用 PropertySourcesPlaceholderConfigurer

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

哪个

From Spring 3.1 onward, the XML will no longer register the old PropertyPlaceholderConfigurer but the newly introduced PropertySourcesPlaceholderConfigurer. This replacement class was created be more flexible and to better interact with the newly introduced Environment and PropertySource mechanism; it should be considered the standard for 3.1 applications.

关于java - 如何共享 2 上下文的属性占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19037586/

相关文章:

java - JFileChooser 不显示

java - 捕获 Spring 中 PropertyEditors 抛出的 IllegalArgumentException

java - Spring boot创建bean时出错

java - 如何在 Spring 测试中仅重新创建选定的 Spring 上下文?

java - Spring MVC 表单标签 - 单选按钮

java - Spring STS初学者学习

java - 将 JSF 与 Spring 集成

java - Java 中的 Rails 闪存消息

java - 为什么这会导致 tomcat 显示 jsp 源而不是实际呈现 html?

JavaFX |用户名和密码检查无法正常工作