我已阅读Injecting Jaxb2Marshaller in Spring MVC controller和Difference between applicationContext.xml and spring-servlet.xml in Spring Framework等,但感觉困惑,为什么我的情况不起作用。无法将servlet上下文中的bean注入到控制器中
以下是我在我的应用程序上下文和web.xml(下调,只显示相关部分):
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app .... >
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/app-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
....
<servlet>
<servlet-name>spring-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
...
</web-app>
弹簧休息-servlet.xml中
<beans ....>
<!-- Only scan for Controllers in Servlet context -->
<context:component-scan base-package="com.fil.ims" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<mvc:annotation-driven />
<context:annotation-config />
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
....
</bean>
</beans>
我有一个控制器是这样的:
@Controller
public class FooControllerImpl implements FooController {
@Inject
@Named("jaxb2Marshaller")
Jaxb2Marshaller jaxb2Marshaller;
public void setJaxb2Marshaller(Jaxb2Marshaller jaxb2Marshaller) {
this.jaxb2Marshaller = jaxb2Marshaller;
}
//.....
}
我发现我无法在servlet上下文中注入jaxb2Marshaller(引发NoSuchBeanDefinitionException
)。但是,如果我把jaxb2Marshaller
放在主应用程序上下文中(app-context.xml
),它可以正常注入。
我很困惑它为什么会发生。根据我的理解,如果我在web.xml
中有ContextLoaderListener
,则提供的上下文配置(app-context.xml)将用于创建顶级应用上下文,然后将调度器servlet上下文(在我的示例中为spring-rest-servlet.xml
)创建为顶级应用上下文中的子上下文。
我不明白的是,控制器和要注入的bean(jaxb2Marshaller)都是在调度程序servlet环境下创建的,为什么我不能在我的FooController
中注入jaxb2Marshaller
? (据我所知,如果要注入的bean位于子应用上下文中,则不起作用,但在这种情况下似乎不是。)
有人可以解释为什么吗?
更新:
我做了另一个实验(这让我更加混乱),在我的控制器实现ApplicationContextAware
,并从控制器,我做applicationContext.getBean("jaxb2Marshaller", Jaxb2Marshaller.class)
。返回有效的bean(与我的理解一致)。似乎只有注射通过注释不起作用。任何线索?