2

我需要创建一个 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)}

Java配置

@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 中使用它。

4

1 回答 1

4

这是FactoryBean@Configuration. 请查看此答案以获取详细信息。

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

@Bean
public SportsdataSoap sportsdataSoap() throws ... {

    return (SportsdataSoap) sportsdata().getObject();
}
于 2013-07-24T20:38:21.093 回答