如何在另一个之前自动装配一个房产?

时间:2015-06-18 01:04:57

标签: spring spring-boot

使用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; 
    } 
}

1 个答案:

答案 0 :(得分:2)

Spring忽略了

static个字段。

除非您使用某种main方法设置应用程序,否则您永远不必直接使用ApplicationContext

在这里,您希望使用它来提取类型为IWCustomerService的bean。相反,让Spring为你注入它。

@Bean 
public EndpointImpl endpoint(IWCustomerService customerService) { 
    EndpointImpl endpoint = 
       new EndpointImpl(cxfBus, customerService); 
    endpoint.publish("/CustomerService"); 
    return endpoint; 
}