您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用web.xml在Spring中加载上下文

使用web.xml在Spring中加载上下文

Spring可以轻松集成到任何基于Java的Web框架中。你需要做的就是在web.xml中声明ContextLoaderListener并使用contextConfigLocation 设置要加载的上下文文件

<context-param>:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

<listener>
   <listener-class>
        org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener> 

然后,你可以使用WebApplicationContext来获取bean的句柄。

WebApplicationContext ctx = WebApplicationContextUtils.getrequiredWebApplicationContext(servlet.getServletContext());
SomeBean someBean = (SomeBean) ctx.getBean("someBean");
Java 2022/1/1 18:13:53 有493人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶