在Servlet中获取spring管理的bean
来源:互联网 发布:置信度传播算法 编辑:程序博客网 时间:2024/05/17 16:46
在Servlet中获取spring管理的bean需要导入spring的web相关的jar包。spring-web-3.2.0.RELEASE.jar
在web.xml文件添加ContextLoaderListener
在Servlet中调用以下代码,获取ApplicationContext对象进而获取spring管理的bean。
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
代码示例:
HelloService.java
package com.my.service;public class HelloService { public void sayHello(){ System.out.println("hello,spring webxxx!"); }}
HelloServlet.java
package com.my.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.ApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import com.my.service.HelloService;public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 从spring容器 获得 HelloService 对象 // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // ApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); HelloService helloService = (HelloService) applicationContext.getBean("helloService"); helloService.sayHello(); }}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:p = "http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="com.my.service.HelloService"></bean></beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <!-- spring 上下文监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring 配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.my.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
阅读全文
0 0
- 在servlet中获取spring管理的bean
- 在Servlet中获取spring管理的bean
- servlet 中获取 spring 管理的 bean
- servlet中获取spring管理的bean
- servlet 中获取 spring 管理的 bean
- Servlet中获取spring bean的管理对象
- Servlet获取Spring管理的Bean
- 在servlet中获取spring Bean对象
- 在Servlet中获取Spring的指定bean
- Servlet中调用Spring管理的Bean
- 在jsp页面中获取Spring管理的Bean
- 如何在线程中获取spring 管理的bean
- 如何在线程中获取spring 管理的bean
- 如何在线程中获取spring 管理的bean
- servlet中手动获取spring的bean
- Spring中ApplicationContextAware实现获取bean的工具类,在servlet中通过servletContext获取application
- 在servlet、filter、Taglib中获取Spring Bean
- 在完全由Spring管理的环境中使用Spring的Context获取Bean实例
- Spring的bean管理(注解注入属性和注解文件混合使用)
- 生成XML
- python获取链家网数据
- 由opencv with qt的imshow源码的一部分联想到在arm上实现highgui
- Service得到配置更改通知
- 在Servlet中获取spring管理的bean
- POJ 1677 Girls' Day 笔记
- FZUOJ 2250 不可能弹幕结界【思维+Bfs】
- Matlab小波包分解后如何求各频带信号的能量值?
- vagrant+centos7 安装报错
- “玲珑杯”线上赛 Round #15 河南专场:E -- 咸鱼旅行
- 《我们仨》读感
- 如何设计短网址系统(TinyURL)
- STM32学习笔记(6): 启动代码(Startup Code)