2013-07-24 107 views
2

我需要創建一個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爲它在以後使用它我控制器也是如此。

回答

4

這是一個FactoryBean@Configuration的互操作性問題。詳情請看at this answer

簡短的版本是顯式添加一個bean到你的配置。

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

    return (SportsdataSoap) sportsdata().getObject(); 
} 
+2

不要忘記在此之前調用'afterPropertiesSet()'。 –

+0

或者我可以修改該方法直接返回SportsdataSoap('return(SportsdataSoap)ret.getObject()')?! – dtrunk

+0

不,Spring需要初始化FactoryBean,這是通過* its *'@ Bean'完成的。如果你在這之前直接調用'getObject()',你會得到一個空AFAIK。 –