使用web.xml方式加载Spring时,获取Spring context的两种方式
2017-10-29 23:13
633 查看
http://blog.csdn.net/rnZuoZuo/article/details/22563103?locationNum=15
使用web.xml方式加载Spring时,获取Spring context的两种方式:
【jsp/servlet】
【jsp/servlet】
使用web.xml方式加载Spring时,获取Spring context的两种方式:
1、servlet方式加载
【web.xml】<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext</param-value> </init-param> </servlet>
【jsp/servlet】
ServletContext context = getServletContext(); XmlWebApplicationContext applicationContext = (XmlWebApplicationContext) context.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet"); DataSource dataSource=(DataSource)applicationContext.getBean("dataSource");
2、listener方式加载
【web.xml】<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
【jsp/servlet】
ServletContext context = getServletContext(); WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(context); DataSource dataSource=(DataSource)applicationContext.getBean("dataSource");
相关文章推荐
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
- 使用web.xml方式加载 Spring时,获取Spring context的两种方式:
- spring的配置文件在web.xml中加载的方式
- spring bean 不使用注入的方式获取的两种方式
- spring的配置文件在web.xml中加载的方式
- spring的配置文件在web.xml中加载的方式
- web.xml方式加载Spring
- 3.Spring Boot + Security初步使用(覆盖配置的两种方式 配置类和XML注入以自定义页面为例)
- Web项目中使用XML配置加载Spring及Spring MVC的组件
- web.xml中配置spring的几种方式 以及 Spring获取Bean的几种方式
- Spring(2)与接口配合使用和获取Bean容器的两种方式(BeanFactory 、ApplicationContext)
- web.xml中配置spring的几种方式 以及 Spring获取Bean的几种方式
- JAVA_WEB项目之使用Spring的xml配置方式在项目中管理Lucene检索框架
- spring的配置文件在web.xml中加载的方式
- web.xml中配置spring的几种方式 以及 Spring获取Bean的几种方式
- web.xml中filter代码中加载spring中定义的bean
- web应用中spring提供了几种加载方式
- spring 中加载xml配置文件的方式
- 使用UI-Tag的注意点: 请确定/WEB-INF/web.xml文件中的ServletDispatcher 设定为自动加载