【问题标题】:Spring JavaConfig + JAX-WS ClientSpring JavaConfig + JAX-WS 客户端
【发布时间】:2013-07-24 20:11:10
【问题描述】:

我需要创建一个 Web 服务客户端来获取 Sportsdata。 但是我在尝试@Autowired sportsdata 时遇到了异常。

例外:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [de.openligadb.schema.SportsdataSoap] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

JavaConfig

@Configuration
@ComponentScan(basePackages = "com.example", excludeFilters = { @Filter(Configuration.class) })
public class MainConfig {
    private @Value("${openligadb.wsdlDocumentUrl}") String wsdlDocumentUrl;
    private @Value("${openligadb.endpointAddress}") String endpointAddress;
    private @Value("${openligadb.namespaceUri}") String namespaceUri;
    private @Value("${openligadb.serviceName}") String serviceName;

    @Bean
    public JaxWsPortProxyFactoryBean sportsdata() throws MalformedURLException {
        JaxWsPortProxyFactoryBean ret = new JaxWsPortProxyFactoryBean();
        ret.setWsdlDocumentUrl(new URL(wsdlDocumentUrl));
        ret.setServiceInterface(SportsdataSoap.class);
        ret.setEndpointAddress(endpointAddress);
        ret.setNamespaceUri(namespaceUri);
        ret.setServiceName(serviceName);
        return ret;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer ret = new PropertySourcesPlaceholderConfigurer();
        ret.setLocation(new ClassPathResource("application.properties"));
        return ret;
    }
}

是的:我知道@PropertySource,但我需要为它创建一个 bean,以便稍后在我的 Controller 中使用它。

【问题讨论】:

    标签: spring jax-ws


    【解决方案1】:

    这是FactoryBean@Configuration 的互操作性问题。详情请查看at this answer

    简短的版本是将 bean 显式添加到您的配置中。

    @Bean
    public SportsdataSoap sportsdataSoap() throws ... {
    
        return (SportsdataSoap) sportsdata().getObject();
    }
    

    【讨论】:

    • 别忘了在那之前可能会打电话给afterPropertiesSet()
    • 或者我可以修改方法直接返回 SportsdataSoap (return (SportsdataSoap) ret.getObject())?!
    • 不,Spring 需要初始化 FactoryBean,这是通过 its @Bean 完成的。如果您在此之前直接致电getObject(),您将获得一个空返回 AFAIK。
    • 谢谢,效果很好! @SotiriosDelimanolis 无需调用 afterPropertiesSet。
    • Spring 4.0.X版本只能定义JaxWsPortProxyFactoryBean bean