2014-03-04 793 views
0

我正在运行一个带有spring-boot的web应用程序,并且必须使用外部连接词,这些连接词使用ContextLoader.getCurrentWebApplicationContext()来获取一些bean。 但它总是返回null。 根据spring-boot文档,父应用程序上下文不是WebApplicationContext。是否有其他方式通过ContextLoader.getCurrentWebApplicationContext()访问WebApplicationContextContextLoader.getCurrentWebApplicationContext()总是返回null

在此先感谢!

回答

5

ContextLoader不适用于Spring Boot应用程序(或任何具有Servlet 3.0动态初始化的应用程序)。 Spring Boot 确实设置了ServletContext以便WebApplicationContextUtils可以定位web上下文。您的外部工件确实不应该使用静态方法,因为它会假定应用程序的部署方式。你能改变它们吗?或者找出另一种方式来设置他们的状态?

+0

不幸的是我无法改变它们。我只是可以发出更改请求。它是关于一些通过'''''ContextLoader'''初始化一些bean的自定义servlet。所以只要他们使用'''''ContextLoader'''我就不得不自己实现这些Servlet? – meltac

+0

我想你可以写一个自定义的'ContextLoaderListener'而不是直接使用'SpringBootServletInitializer'或'SpringApplication'。监听器将扩展'ContextLoader'(或现有的监听器),但使用'SpringApplication'加载上下文。这里有一个原型(https://github.com/scratches/spring-boot-legacy/blob/master/src/main/java/org/springframework/boot/legacy/context/web/SpringBootContextLoaderListener.java)if你有兴趣。 –

+0

非常感谢!这将是一个很大的帮助。 – meltac