使用spring boot,如何自动连接applicationContext?
必须在调用endpoint()
@Configuration
@EnableTransactionManagement
@EnableAutoConfiguration
@ComponentScan({"com.dev.core.services", "com.dev.core.wservices"})
@ImportResource({ "classpath:META-INF/cxf/cxf.xml" })
public class ContextConfig extends SpringBootServletInitializer {
@Autowired
private static ApplicationContext applicationContext;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ContextConfig.class);
}
@Bean
public EndpointImpl endpoint() {
// applicationContext is null how to fix that ?
EndpointImpl endpoint =
new EndpointImpl(cxfBus, applicationContext.getBean(IWCustomerService.class) );
endpoint.publish("/CustomerService");
return endpoint;
}
}
答案 0 :(得分:2)
static
个字段。
除非您使用某种main
方法设置应用程序,否则您永远不必直接使用ApplicationContext
。
在这里,您希望使用它来提取类型为IWCustomerService
的bean。相反,让Spring为你注入它。
@Bean
public EndpointImpl endpoint(IWCustomerService customerService) {
EndpointImpl endpoint =
new EndpointImpl(cxfBus, customerService);
endpoint.publish("/CustomerService");
return endpoint;
}