java 应用上下文 xml 文件中的 Spring util 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32715190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:39:50  来源:igfitidea点击:

Spring util properties in app-context xml file

javaxmlspring

提问by Mithrand1r

I am describing properties file in app-context.xmllike this:

我正在这样描述属性文件app-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:task="http://www.springframework.org/schema/task"
    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/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd">


    <context:annotation-config></context:annotation-config>

    <task:annotation-driven />


    <util:properties id="properties" location="file:/opt/vortal/VortalDataProvider/config/votral.properties"></util:properties>

</beans>

in any java file I am able to access this file through:

在任何 java 文件中,我都可以通过以下方式访问此文件:

@Autowired
@Qualifier("properties")
private Properties properties;

However when I try to access properties in camel-context.xml

但是,当我尝试访问属性时 camel-context.xml

like:

喜欢:

<cxf:rsClient id="vortalEndpoint" address="${endpointUrl}" 
     serviceClass="pl.test.dataprovider.Processor"
     skipFaultLogging="false"/> 

value of bean vortalEndpointis literally ${endpointUrl}- value from poperties is not read.

bean 的值vortalEndpoint是字面意思${endpointUrl}- 不读取来自 poperties 的值。

What am I doing wrong?

我究竟做错了什么?

采纳答案by Jaiwo99

<util:properties>

initialized an instance of java.util.Propertiesclass, check out the Doc. This is not the proper way to load your properties, if you are doing it in xml, you can use

初始化了一个java.util.Properties类的实例,查看Doc。这不是加载属性的正确方法,如果您在 xml 中执行此操作,则可以使用

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

Then you can access your properties easily with

然后您可以轻松访问您的属性

 @Value("${whatever}")
 private String myValue