【问题标题】:how to Autowire a property before another?如何在另一个之前自动装配一个属性?
【发布时间】:2015-06-18 01:04:57
【问题描述】:

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

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    static 字段被 Spring 忽略。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      相关资源
      最近更新 更多