【问题标题】:Spring - Using property-placeholder to load properties file that is outside the war file or the classpath but is in the filesystemSpring - 使用属性占位符加载位于war文件或类路径之外但在文件系统中的属性文件
【发布时间】:2012-06-23 20:48:12
【问题描述】:

我已经配置了属性文件的加载,如下图(Spring 3.1)

我的-spring-xml

<context:component-scan base-package="com.mypackage"/>
<context:property-placeholder location="file:///C:/temp/application.properties"/>

web.xml

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

application.properties

expirationOffset = 777

在我的 java 类中,我声明了如下属性:

private @Value("${expirationOffset}") String propertyValue;

由于某种原因,该值没有被初始化。当我运行以下语句时:

System.out.println("VALUE - " + propertyValue);

输出总是

16:03:02,355 INFO  [stdout] (http--127.0.0.1-8080-1) VALUE - ${expirationOffset}

我试图将属性文件移动到 war 文件中,以便在它位于类路径中时尝试访问它,但仍然是同样的问题。这是我从类路径访问它的方式。

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

理想情况下,我希望将属性文件保留在 war 文件之外,因为我不想因为简单的属性更改而不得不重建 war 文件。

我错过了什么吗?

编辑

我从这个上下文中删除了 msm-spring.xml 初始化参数:

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

并将其移至 servlet 上下文,如下所示。

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>myservice</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            /WEB-INF/my-spring-xml
            </param-value>          
        </init-param>
    </servlet>

上述更改似乎已经修复,因为我现在得到了正确的值。我认为我最初的方式是将它放在应用程序上下文中,这意味着它将在整个应用程序/webapp 中可用。

在上面列出的 3 个中使用 msm-spring 有什么区别?

【问题讨论】:

    标签: java spring spring-mvc properties autowired


    【解决方案1】:

    编辑:

    您是否尝试使用 #{expirationOffset} 的 setter 方法??

    即:

    private String propertyValue;
    @Value("#{expirationOffset}")
        public void setPropertyValue(String property) {
            propertyValue = property;
        }
    

    另一种选择:

    像这样添加 Properties bean 而不是 PropertyPlaceConfigurer:

    <util:properties id="myProps" location="file:///C:/temp/application.properties" />
    

    <util:properties id="myProps" location="classpath:application.properties" />
    

    并将 Setter 稍作修改替换为

    private String propertyValue;
    @Value("#{myProps.expirationOffset}")
        public void setPropertyValue(String property) {
            propertyValue = property;
        }
    

    您必须在上下文配置 xml 中添加 xmlns:util="http://www.springframework.org/schema/util"xmlns 贴花和对应的 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsdxsi:schemalocation

    这绝对可以工作。!

    希望对您有所帮助。 :)

    【讨论】:

    • 我刚刚尝试过,它正在打印“VALUE - #@Value${expirationOffset}”作为输出。在我看来,spEL 没有被解释。
    • 我的错,再次检查答案.. :( '@Value' 内不应该有 '@Value' :(
    【解决方案2】:

    这是根据您提供的信息进行的一些推测:

    您的根 Web 应用程序上下文中可能没有 &lt;context:property-placeholder.. - 由 ContextLoaderListener 加载的那个,相反,您可能在 Web 上下文中拥有它(由 Dispatcher servlet 加载)。请您确认一下。

    更新:根据评论,问题似乎是 &lt;context:propert-placeholder.. 是在根 Web 应用程序上下文中定义的,但在 Web 上下文的组件中被引用。

    在这种情况下,解决方法是将属性占位符移动到 Web 上下文(通过 MessageDispatcherServlet 定义的上下文)。

    【讨论】:

    • 我反其道而行之。 property-placeholder 被定义为 的参数值,但不是 org.springframework.ws.transport.http.MessageDispatcherServlet servlet 的
    • 我移动了它,现在可以看到值了。请参阅原始帖子中的编辑。
    • 酷,我已经根据这些信息更新了我的答案。这不起作用的原因可能是因为 PropertyPlaceHolderConfigurer 是一个 BeanFactoryPostProcessor 并在定义它的上下文中解析占位符 - 在这种情况下是根上下文,并且不能为 Web 上下文中的占位符这样做.
    • 因此,如果我想从 Root Web 应用程序上下文配置中加载属性并让 [Dispatcher]Servlet 上下文中的 bean 被注入这些属性值,那么没有办法做到这一点吗?是否建议我在定义/扫描使用这些属性的 bean 的同一配置文件中加载属性文件?
    猜你喜欢
    • 2019-02-12
    • 2017-01-24
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多