我有一个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从未设置。

最佳答案

首先,您的问题与Spring Integration无关,因此选择问题标签时要小心。

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

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

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

09-12 04:24