在Web.xml中将HttpServlet与Spring Application Context连接,可以通过以下步骤实现:
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<bean id="myHttpServlet" class="com.example.MyHttpServlet">
<!-- 在这里添加需要的依赖注入 -->
</bean>
<servlet-name>MyHttpServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet><servlet-mapping>
<servlet-name>MyHttpServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean id="myHttpServlet" class="com.example.MyHttpServlet">
<property name="context" ref="applicationContext"/>
</bean>
public class MyHttpServlet extends HttpServlet implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
// 在这里可以使用applicationContext获取其他Bean的引用
}
通过以上步骤,可以在Web.xml中将HttpServlet与Spring Application Context连接。
领取专属 10元无门槛券
手把手带您无忧上云