更新时间:2022-12-22 12:11:51
我认为您可以创建自己的 ContextLoaderListener
并覆盖 createWebApplicationContext
返回JavaSE应用程序的ApplicationContext(假设你有某种ApplicationContextHolder来保持对它的静态访问)。
I think you can create your own ContextLoaderListener
and overriding createWebApplicationContext
to return the ApplicationContext of your JavaSE app (assuming you have some kind of ApplicationContextHolder to keep a static access to it).
看起来很简单,但你需要返回一个 WebApplicationContext
而不是 ClassPathXmlApplicationContext
来做这个,我想你可以做这样的事情:
It seems easy, but you need to return a WebApplicationContext
and not a ClassPathXmlApplicationContext
to do this, I think you can do something like this:
public class MyContextLoaderListener extends ContextLoaderListener{
protected WebApplicationContext createWebApplicationContext(ServletContext sc,
ApplicationContext parent){
ApplicationContext javaSEAppContext = AppContextHolder.getAppContext();
GenericWebApplicationContext context = new GenericWebApplicationContext(servletContext);
context.setParent(javaSEAppContext);
return context;
}
}
当然要调整你的web.xml
And of course adapt your web.xml
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param> -->
<listener>
<listener-class>com.company.MyContextLoaderListener</listener-class>
</listener>