加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:13:17 所属栏目:大数据 来源:网络整理
导读:1.需要引入额外的jar包: spring-web spring-webmvc 2.spring的配置文件没什么区别。 3.如何创建IOC容器? 非web应用在main方法中直接创建; 在web应用中应该在被服务器加载时就创建; 在ServletContextListener#contextInitialized(ServletContextEvent sce

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容器是成功的。?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读