微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring之如何在web应用中使用?

1.需要引入额外的jar包

  • spring-web
  • spring-webmvc

2.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
 *
 */
@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容器是成功的。 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐