在Servlet中获取spring管理的bean详解编程语言
2023-06-13 09:20:36 时间
在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();?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" /bean /beansweb.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原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/12101.html
cjavaxml
相关文章
- Spring学习笔记(五)——JdbcTemplate和spring中声明式事务
- Spring 全家桶之 Spring Boot 2.6.4(八)- 嵌入式 Servlet 容器(Part B)
- 慎用ApplicationContextAware的实现类来操作Spring容器的bean
- Spring Cloud Tencent 1.7 版本最新发布
- Spring Boot基于JWT的用户实时在线数获取方法
- Spring MVC框架:第二章:视图解析器和@RequestMapping注解使用在类级别及获取原生Servlet API对象
- Spring Boot凭什么成为JVM圈的框 架“一哥”
- spring官方文档 中文_Spring软件
- Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
- Spring监听器-spring源码详解(五)
- Spring Boot的配置文件
- Spring Cloud Stream核心组件Binder(二)
- Spring Cloud Security的核心组件-Cloud Security Filter
- jpa+spring配置多数据源详解编程语言
- Spring Cloud之Swagger集群搭建详解编程语言
- spring boot中servlet启动原理详解编程语言
- Spring Boot 主类及目录结构介绍详解编程语言
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理详解编程语言
- Spring Boot(五):spring data jpa的使用详解编程语言
- Spring MVC的转发与重定向