Spring 属性占位符未在 jaxws:client (cxf) 地址属性中解析

vik*_*y S 6 spring cxf spring-mvc jax-ws spring-cxf

Environment:

    Spring MVC : 4.1.7.RELEASE
    CXF: 3.0.0
    java: 1.8

web.xml --- loads appContext.xml (spring cofigs) & cxfContext.xml (configs for cxf)

spring-servlet.xml --- loading the spring mvc configs.
Run Code Online (Sandbox Code Playgroud)

我正在使用以下方式加载属性文件。

@Configuration

    @PropertySource(value = { "classpath:config.properties" })
    public class Configuration {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    }
Run Code Online (Sandbox Code Playgroud)

属性正在得到解决,除了一种情况之外没有任何问题。

我将 CXF 用于 Web 服务,并且在使用时地址属性没有得到解析"${addressVal}"。xml 中的所有其他属性都已加载,除了"jaxws:client".

<jaxws:client id="port"
        serviceClass="com.service.Myclass"
        address="${addressVal}" />
Run Code Online (Sandbox Code Playgroud)
  1. 问题出在哪儿。我做错了什么。

  2. servlet 上下文/应用程序上下文加载问题?

请指教。

小智 3

我有同样的问题。遗憾的是还没有找到解决方案。但是,对于发现此问题的任何人,解决方法是使用 JaxWsProxyFactoryBean。

例子:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass" value="demo.spring.service.HelloWorld"/>
    <property name="address" value="${some.property.value}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

它不是那么好,因为你必须注入工厂,调用 create() 并强制转换,但至少它可以工作。

@Autowired
@Qualifier("clientFactory")
private JaxWsProxyFactoryBean factory;

public void callService() {
    HelloWorld helloWorld = (demo.spring.service.HelloWorld)factory.create();
}
Run Code Online (Sandbox Code Playgroud)

您还可以将以下内容添加到 spring 配置中以创建特定的 bean,但这对我不起作用。尝试注入该 bean 失败,这就是我选择上述方法的原因。

<bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
Run Code Online (Sandbox Code Playgroud)

另请参阅页面底部的http://cxf.apache.org/docs/writing-a-service-with-spring.html