在ClientInterceptor中使用配置文件属性

时间:2016-09-27 15:29:30

标签: java spring

我有profiles.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: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/util
                           http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Local -->
    <beans profile="local">
        <util:properties id="localProperties">
            <prop key="property">localProperty</prop>
        </util:properties>
        <context:property-placeholder properties-ref="localProperties" ignore-unresolvable="true" />
    </beans>

    <!-- Dev -->
    <beans profile="dev">
        <util:properties id="devProperties">
            <prop key="property">devProperty</prop>
        </util:properties>
        <context:property-placeholder properties-ref="devProperties" ignore-unresolvable="true" />
    </beans>

</beans>

我有一个org.springframework.ws.client.support.interceptor.ClientInterceptor,我想使用profiles.xml中的值:

@Component
public class HeaderInjector implements ClientInterceptor {

    @Value("${property}")
    private static String someProperty;

    @Override
    public boolean handleRequest(MessageContext messageContext)
            throws WebServiceClientException {
        //want to use someProperty here based on value from profiles.xml
    }

}

我该怎么做?我尝试在类的顶部添加@ImportResource("profiles.xml"),如

@Component
@ImportResource("profiles.xml")
public class SoapLeadPipeHeaderInjector implements ClientInterceptor {

someProperty永远不会被设置。

1 个答案:

答案 0 :(得分:1)

首先,您的问题中没有Spring Integration,因此请谨慎选择问题标签。

如果从注释启动应用程序上下文,则可以在@ImportResource("profiles.xml")类上应用

@Configuration

如果您的主要入口点是XML配置,则必须通过@Component扫描<context:component-scan base-package="..."/>

在Spring Framework Reference Manual中查看更多信息。