我有一个 Spring beans 定义文件,如下
<bean id="jettyZk" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
<!-- properties, threadPool, connectors -->
<property name="handler">
<bean class="org.eclipse.jetty.servlet.ServletContextHandler">
<property name="eventListeners">
<list>
<!-- my.ContextLoaderListener
* An ApplicationContextAware ContextLoaderListener that allows for using the current ApplicationContext,
* as determined by ApplicationContextAware, as the parent for the Root WebApplicationContext.
*
* Also provides for specifying the contextConfigLocation of the Root WebApplicationContext, because
* Eclipse Jetty 7 ServletContextHandler does not expose a setInitParameters method.
-->
<bean class="my.ContextLoaderListener">
<property name="contextConfigLocation" value="/META-INF/spring/applicationContext-securityZk.xml"/>
</bean>
<!-- not sure if this is needed, disabled for now -->
<!-- <bean class="org.springframework.web.context.request.RequestContextListener"/> -->
</list>
</property>
<property name="servletHandler">
<bean class="org.eclipse.jetty.servlet.ServletHandler">
<property name="filters">
<list>
<bean class="org.eclipse.jetty.servlet.FilterHolder">
<property name="name" value="springSecurityFilterChain"/>
<property name="filter">
<bean class="org.springframework.web.filter.DelegatingFilterProxy"/>
</property>
</bean>
</list>
</property>
<!-- filterMappings, servlets, servletMappings -->
</bean>
</property>
</bean>
</property>
</bean>
尝试启动上下文时,出现以下异常
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: ServletContext must not be null
Caused by: java.lang.IllegalArgumentException: ServletContext must not be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:109)
这是使用 Spring 3.0.5.RELEASE
异常是可以理解的,查看 DelegatingFilterProxy#findWebApplicationContext 的代码和 JavaDocs,上面写着
在此过滤器被初始化(或调用)之前,WebApplicationContext 必须已经加载并存储在 ServletContext 中。
因为我试图将过滤器创建为上下文处理程序的(子)属性,所以上下文尚未初始化似乎是明智的,因此 Spring WAC 也没有。
我想知道的是如何在 Spring 本身正在组装的嵌入式 Jetty 容器中配置 Spring Security?
似乎有一个 catch 22 场景只需要后期初始化,但我找不到合适的标志来旋转。我尝试过设置lazy-init="true"
过滤器 bean,但这似乎并没有取得太大的成就,这不足为奇。