我需要創建一個web服務客戶端來獲取sportsdata。 但我在嘗試@Autowired sportsdata
時遇到異常。Spring JavaConfig + JAX-WS客戶端
例外:
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爲它在以後使用它我控制器也是如此。
不要忘記在此之前調用'afterPropertiesSet()'。 –
或者我可以修改該方法直接返回SportsdataSoap('return(SportsdataSoap)ret.getObject()')?! – dtrunk
不,Spring需要初始化FactoryBean,這是通過* its *'@ Bean'完成的。如果你在這之前直接調用'getObject()',你會得到一個空AFAIK。 –