[英]Not able to load Application Context in Spring Boot app
I am working on a Spring Boot application wherein I am using that application to expose a SOAP webservice. 我正在使用Spring Boot应用程序,其中正在使用该应用程序公开SOAP Web服务。 I am using Apache CFX framework for SOAP impl in Spring boot app. 我在Spring Boot应用程序中将Apache CFX框架用于SOAP impl。 I am using Annotation based approach. 我正在使用基于注释的方法。
I am facing issue in setting the Application Context from the Spring Boot Configuration file in one of the Beans. 我在通过Beans之一中的Spring Boot Configuration文件设置应用程序上下文时遇到问题。 Below is my code. 下面是我的代码。
@SpringBootApplication
@ComponentScan("com.test")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The configuration file is as below. 配置文件如下。
@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;
}
Now I have the bean SOAPprocessImpl implementation in which I need to get the Application Context so that I can get handle to the Validator bean. 现在,我有了bean SOAPprocessImpl实现,需要在其中实现应用程序上下文,以便可以获取Validator bean的句柄。 I have declared SOAPprocessImpl as a bean in the configuraton file. 我已经在配置文件中将SOAPprocessImpl声明为bean。 The code is as below 代码如下
@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
}
} }
So the issue is that when I run the boot application by deploying to the embedded Tomcat then the Application Context is not getting set in the SOAPprocessImpl class even after implementing the ApplicationContextAware. 因此,问题在于,当我通过部署到嵌入式Tomcat来运行启动应用程序时,即使在实施ApplicationContextAware之后,也不会在SOAPprocessImpl类中设置应用程序上下文。 I also tried Autowiring but that also is not working. 我也尝试了自动装配,但是也没有用。
Strangely I tried to see if I can get the ApplicationContext in the Configuration file where all the bean are defined. 奇怪的是,我尝试查看是否可以在定义了所有bean的配置文件中获取ApplicationContext。 Here it is getting setting properly. 在这里它正在正确设置。
Can anyone help me how to solve this issue. 谁能帮我解决这个问题。 I am new to Spring Boot and may have missed some configutaion. 我是Spring Boot的新手,可能已经错过了一些配置。 Thanks in advance. 提前致谢。
Option(1): To fix the issue, you need to use @Configuration
to register your SOAPprocessImpl
bean to the Spring container as shown below so that ApplicationContext
object can be injected : 选项(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
}
The important point is that you can't use the context
object until the bean is prepared by the container, so you need to use @PostConstruct
method as shown above to initialise your variables. 重要的一点是,在容器准备好bean之前,您不能使用context
对象,因此您需要使用如上所示的@PostConstruct
方法来初始化变量。
Option2 (recommended): The best approach is that you can use @Autowired
to inject IValidator
object into SOAPprocessImpl
as shown below so that you don't need your SOAPprocessImpl
bean to be aware of ApplicationContextAware
. Option2(推荐):最好的方法是,可以使用@Autowired
将IValidator
对象注入到SOAPprocessImpl
,如下所示,这样您就不需要SOAPprocessImpl
bean来了解ApplicationContextAware
。 Spring container will inject the instance for the implementation provided for the IValidator
class (provided it is under the packages of @Componentscan
). 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.