栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring-集成Web环境

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring-集成Web环境

Spring学习笔记目录

目录
  • 步骤
  • 代码示例
    • 导入需要的坐标
    • 运行图片
  • ApplicationContext应用上下文对象
    • 概念
    • 代码示例
    • 运行结果
    • 改进
      • 运行结果
  • Spring提供获取应用上下文的工具
    • 概念
    • 步骤
    • 运行结果

步骤

①导入需要的坐标

代码示例

项目结构:

导入需要的坐标

        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
            javax.servlet
            javax.servlet-api
            3.0.1
            provided
        
        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.2.1
        
    

新建接口UserDao.java:

package com.stu.dao;


public interface UserDao {
    public void save();
}

新建实现类UserDaoImpl .java:

package com.stu.dao.impl;

import com.stu.dao.UserDao;


public class UserDaoImpl implements UserDao {

    @Override
    public void save() {
        System.out.println("save runing");
    }
}

新建接口类UserService.java:

package com.stu.service;


public interface UserService {
    public void save();
}

新建实现类UserServiceImpl.java:

package com.stu.service.impl;

import com.stu.dao.UserDao;
import com.stu.service.UserService;


public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save(){
        userDao.save();
    }
}

新建配置文件ApplicationContext.xml:



    
    
        
    

新建类UserServlet.java:

package com.web;

import com.stu.service.UserService;
import com.stu.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean(UserService.class);
        userService.save();
    }
}

在web.xmnl中新增以下代码:

    
        UserServlet
        com.web.UserServlet
    
    
    
        UserServlet
        /userServlet
    


配置Tomcat


运行图片

运行tomcat后,在网址区域输入http://localhost:8080/userServlet在控制台打有以下结果就说明成功。

ApplicationContext应用上下文对象 概念

在Web项目中,可以使用ServletContextListener监听Web应用的启动,在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。——黑马程序员

代码示例


新建类ContextLoaderListener.java:

package com.stu.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class ContextLoaderListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //将Spring的应用上下文对象存储到Servlet域中
        ServletContext servletContext = servletContextEvent.getServletContext();//获得ServletContext
        servletContext.setAttribute("applicationContext",applicationContext);//通过ServletContext设置属性
        System.out.println("spring容器创建完毕...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

类UserServlet.java新增以下代码:

//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        req.getServletContext();
ServletContext servletContext = this.getServletContext();
        ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");


web.xml中新增以下代码:

    
        com.stu.listener.ContextLoaderListener
    

运行结果

在浏览器的网址中输入http://localhost:8080/userServlet

改进


这两处在代码中是已经写定了,耦合程度高,接下来进行解耦操作。
项目结构:

web.xml新增以下配置:


    
        contextConfigLocation
        applicationContext.xml
    

报红没关系,不管它。
ContextLoaderListener.java的contextInitialized()方法内的代码更改为以下:

        //将Spring的应用上下文对象存储到Servlet域中
        ServletContext servletContext = servletContextEvent.getServletContext();//获得ServletContext
        //读取web.xml中的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(contextConfigLocation);
        servletContext.setAttribute("applicationContext",applicationContext);//通过ServletContext设置属性
        System.out.println("spring容器创建完毕...");

新建类WebApplicationContextUtils:

package com.stu.listener;

import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;


public class WebApplicationContextUtils {
    public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("applicationContext");
    }
}

UserServlet.java新增以下代码:

//        ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");
        ServletContext servletContext = this.getServletContext();


这样写就不需要总是记住文件中指定的applicationContext的名,该方法会直接返回。

运行结果

在浏览器的网址中输入http://localhost:8080/userServlet

在控制台中打印出以下结果说明配置成功。

Spring提供获取应用上下文的工具 概念

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。——黑马程序员

步骤

①在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
②使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
在pom.xml新增以下坐标:

	 
            org.springframework
            spring-web
            5.0.5.RELEASE
        

web.xml新增以下配置:

classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener


UserServlet的doGet方法替换为以下:

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        req.getServletContext();
        ServletContext servletContext = this.getServletContext();
//        ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");
//        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = applicationContext.getBean(UserService.class);
        userService.save();
    }
运行结果

运行tomcat后,在网址区域输入http://localhost:8080/userServlet在控制台打有以下结果就说明成功。

Tomcat正常运行,且控制台有打印对应结果。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/299179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号