【问题标题】:Not able to load Application Context in Spring Boot app无法在 Spring Boot 应用程序中加载应用程序上下文
【发布时间】:2017-03-29 11:21:08
【问题描述】:

我正在开发一个 Spring Boot 应用程序,我正在使用该应用程序来公开一个 SOAP Web 服务。我在 Spring Boot 应用程序中使用 Apache CFX 框架进行 SOAP impl。我正在使用基于注释的方法。

我在从其中一个 Bean 中的 Spring Boot 配置文件中设置应用程序上下文时遇到问题。下面是我的代码。

@SpringBootApplication
@ComponentScan("com.test")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
   }
}

配置文件如下。

@Configuration
public class WebServiceConfiguration {

//All individual bean definitions should go here
@Autowired
ApplicationContext appContext;

@Bean
public ServletRegistrationBean cxfServlet() {
    return new ServletRegistrationBean(new CXFServlet(), "/soap-api/*");
}

@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
    return new SpringBus();
}


@Bean(name="IValidator")
public IValidator getValidator(){
    return new Validator();
}

@Bean(name="SOAPprocessImpl")
public IPSoap getService() {
    return new SOAPprocessImpl();
}

@Bean
public Endpoint endpoint() {
    EndpointImpl endpoint = new EndpointImpl(springBus(), getService());
    endpoint.publish("/WS_1.0");
    endpoint.setWsdlLocation("process.wsdl");
    return endpoint;
}

现在我有了 bean SOAPprocessImpl 实现,我需要在其中获取应用程序上下文,以便获得 Validator bean 的句柄。我已在配置文件中将 SOAPprocessImpl 声明为 bean。代码如下

@javax.jws.WebService (endpointInterface="com.test.IPSoap")
public class SOAPprocessImpl implements IPSoap, ApplicationContextAware {

private static ApplicationContext context;

public static ApplicationContext getApplicationContext() {
    return context;
}

@Override
public void setApplicationContext(ApplicationContext ac)
        throws BeansException {
    context = ac;
}

private static final Logger logger = Logger.getLogger(SOAPprocessImpl.class.getName());

private IValidator validator = (IValidator) context.getBean("IValidator"); // context is NULL here

public IRResponse GetBalance(TSSearchParams SearchParams) {

    // Some processing logic
    }

}

所以问题是,当我通过部署到嵌入式 Tomcat 来运行启动应用程序时,即使在实现 ApplicationContextAware 之后,也没有在 SOAPprocessImpl 类中设置应用程序上下文。我也尝试过自动装配,但这也不起作用。

奇怪的是,我试图查看是否可以在定义了所有 bean 的配置文件中获取 ApplicationContext。这里设置正确。

谁能帮我解决这个问题。我是 Spring Boot 的新手,可能错过了一些配置。提前致谢。

【问题讨论】:

    标签: java spring web-services soap spring-boot


    【解决方案1】:

    Option(1): 要解决此问题,您需要使用@Configuration 将您的SOAPprocessImpl bean 注册到 Spring 容器,如下所示,以便ApplicationContext 对象可以被注入:

    @Configuration
    @javax.jws.WebService (endpointInterface="com.test.IPSoap")
    public class SOAPprocessImpl implements IPSoap, ApplicationContextAware {
    
       private static ApplicationContext context;
    
       private IValidator validator;
    
       public static ApplicationContext getApplicationContext() {
         return context;
       }
    
       @Override
       public void setApplicationContext(ApplicationContext ac)
          throws BeansException {
          SOAPprocessImpl.context = ac;
       }
    
    
       @PostConstruct//use PostConstruct
       public void init() {
         validator = (IValidator) context.getBean("IValidator");
       }
    
    
       //add your current code
    }
    

    重要的一点是,在容器准备好bean之前,你不能使用context对象,所以你需要使用如上所示的@PostConstruct方法来初始化你的变量。

    选项 2(推荐): 最好的方法是您可以使用@AutowiredIValidator 对象注入SOAPprocessImpl,如下所示,这样您就不需要SOAPprocessImpl bean 来了解ApplicationContextAware。 Spring 容器将为 IValidator 类提供的实现注入实例(前提是它在 @Componentscan 的包下)。

    @Component
    @javax.jws.WebService (endpointInterface="com.test.IPSoap")
    public class SOAPprocessImpl implements IPSoap {
    
         private static final Logger logger = Logger.getLogger(SOAPprocessImpl.class.getName());
    
    
         @Autowired //spring directly injects this object
         private IValidator validator;
    
         public IRResponse GetBalance(TSSearchParams SearchParams) {
            // Some processing logic
        }
    }
    

    【讨论】:

    • 谢谢。我尝试了您的方法,但仍然无法正常工作。上下文仍然为空。
    • SOAPprocessImpl 驻留在哪个包?
    • SOAPprocessImpl 位于不同的包中。 com.test.config 和 WebServiceConfiguration 位于其他包中。 com.test.ws
    • 非常感谢您的清晰解释。它现在可以工作,我可以从您建议的第二种方法中获取验证器实例。祝你有个美好的一天,我的朋友。
    猜你喜欢
    • 2018-02-26
    • 1970-01-01
    • 2019-06-16
    • 2014-08-18
    • 1970-01-01
    • 2020-07-23
    • 2011-05-13
    • 1970-01-01
    • 2021-07-27
    相关资源
    最近更新 更多