spring之如何在web应用中使用?
1.需要引入额外的jar包:
2.spring的配置文件没什么区别。 3.如何创建IOC容器?
4.在WEB应用中其它组件如何来访问IOC容器呢? 可以将IOC容器放在ServletContext(即applicaiton域)的一个属性中。 5.实际上spring配置文件的名字和位置也是可以配置的。将其配置到当前web应用的初始化参数中较为合适。 实际操作: 新建一个动态的java web项目 需要注意的是不要直接按finish,要一直到最后将web.xml文件导入进来。 将相关的java包放入到WEB-INF的lib下。 相关目录如下: Person.java package com.gong.spring.struts2.beans; public class Person { private String username; void setUsername(String username) { this.username = username; } hello(){ System.out.println("My name is " + username); } } SpringServletContextListener.java com.gong.spring.struts2.listeners; import javax.servlet.ServletContext; javax.servlet.ServletContextEvent; javax.servlet.ServletContextListener; javax.servlet.annotation.WebListener; org.springframework.context.ApplicationContext; org.springframework.context.support.ClassPathXmlApplicationContext; /** * Application Lifecycle Listener implementation class SpringServletContextListener * */ @WebListener class SpringServletContextListener implements ServletContextListener { * Default constructor. */ public SpringServletContextListener() { // TODO Auto-generated constructor stub } * @see ServletContextListener#contextDestroyed(ServletContextEvent) contextDestroyed(ServletContextEvent arg0) { TODO Auto-generated method stub ServletContextListener#contextInitialized(ServletContextEvent) contextInitialized(ServletContextEvent arg0) { TODO Auto-generated method stub 1.获取spring配置文件的名称 ServletContext servletContext = arg0.getServletContext(); String config = servletContext.getInitParameter("contextConfigLocation"); 2..创建IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext(config); 3.将IOC容器放在ServletContext的一个属性中 servletContext.setAttribute("ApplicationContext",ctx); } } TestServlet.java com.gong.spring.struts2.servlets; java.io.IOException; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; org.springframework.context.ApplicationContext; com.gong.spring.struts2.beans.Person; * Servlet implementation class TestServlet */ class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response) protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { 1. 从 application 域对象中得到 IOC 容器的引用 ServletContext servletContext = getServletContext(); ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext"); 2. 从 IOC 容器中得到需要的 bean Person person = ctx.getBean(Person.); person.hello(); } } applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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="person" class="com.gong.spring.struts2.beans.Person"> property name="username" value="gong"></property> </bean> beans> web.xml web-app xmlns:xsi xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"display-name>ssm<!-- 加载springIOC容器 --> context-paramparam-name>contextConfigLocationparam-value>applicationContext.xml 启动IOC容器的ServletContextListener listenerlistener-class>com.gong.spring.struts2.listeners.SpringServletContextListenerservletdescription>TestServletservlet-nameservlet-class>com.gong.spring.struts2.servlets.TestServletservlet-mappingurl-pattern>/TestServletwelcome-file-listwelcome-file>index.jspweb-app> index.jsp %@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"htmlheadmeta http-equiv="Content-Type" content="text/html; charset=UTF-8"title>Insert title herebody> a href="TestServlet"a> > 启动tomcat服务器之后,访问http://localhost:8080/myspring5/index.jsp 点击: 在终端输出: 说明在WEB应用中配置和使用springIOC容器是成功的。? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 【OCP-12c】2019年CUUG OCP 071考试题库(74题)
- mmonit golang重启慢和状态不存在
- 【原创】源码解析 packetbeat 之 responsetime 计
- 机房收费系统——VB将MSHflexgrid控件中的数据导
- delphi – Indy TCPClient OnDisconnect事件无法
- Go 1.1 Release Candidate 1 released[golang-nu
- delphi – Firebird:DBExpress还是原生组件?
- Delphi 获取网站验证码的图片
- Inno Setup-----------捆绑插件与更改快捷方式图
- 【PPT版】大数据下的用户分析,彻底挖掘你的用户