如何在另一个之前自动连接 属性?
how to Autowire a property before another?
使用spring引导,如何自动装配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;
}
}
static
字段被 Spring 忽略。
除非您使用某种 main
方法设置您的应用程序,否则您永远不必直接使用 ApplicationContext
。
在这里,您想使用它来提取类型为 IWCustomerService
的 bean。相反,让 Spring 为您注入它。
@Bean
public EndpointImpl endpoint(IWCustomerService customerService) {
EndpointImpl endpoint =
new EndpointImpl(cxfBus, customerService);
endpoint.publish("/CustomerService");
return endpoint;
}
使用spring引导,如何自动装配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;
}
}
static
字段被 Spring 忽略。
除非您使用某种 main
方法设置您的应用程序,否则您永远不必直接使用 ApplicationContext
。
在这里,您想使用它来提取类型为 IWCustomerService
的 bean。相反,让 Spring 为您注入它。
@Bean
public EndpointImpl endpoint(IWCustomerService customerService) {
EndpointImpl endpoint =
new EndpointImpl(cxfBus, customerService);
endpoint.publish("/CustomerService");
return endpoint;
}