1.需要引入额外的jar包:
spring-webspring-webmvc2.spring的配置文件没什么区别。
3.如何创建IOC容器?
非web应用在main方法中直接创建;在web应用中应该在被服务器加载时就创建;在servletcontextlistener#contextinitialized(ServletContextEvent sce)方法中创建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 * */@WebListenerclass 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容器是成功的。
总结以上是内存溢出为你收集整理的spring之如何在web应用中使用?全部内容,希望文章能够帮你解决spring之如何在web应用中使用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)