zl程序教程

您现在的位置是:首页 >  后端

当前栏目

在Servlet中获取spring管理的bean详解编程语言

SpringServlet编程语言 详解 管理 获取 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 

 /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 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/12101.html

cjavaxml