【SSM基础知识7】Spring整合web环境

目录

Javaweb三大组件及环境特点

Spring整合web环境


在Java语言范畴内;web层框架都是基于Javaweb基础组件完成的

Servlet :服务端小程序,负责接收客户端 请求并作出响应的

单例对象,默认第一次访问创建,可以通过配置指定服务器启动就创建,Servlet 创建完毕会执行初始化init方法。每个Servlet有一个service方法,每次访问都会执行service方法,但是缺点是一个业务功能就需要配置一个Servlet 

Filter :过滤器,负责对客户端请求进行过滤操作的

单例对象,服务器启动时就创建,对象创建完毕执行init方法,对客户端的请求进行过滤,符合要求的放行,不符合要求的直接响应客户端,执行过滤的核心方法doFilter

Listener: 监听器,负责对域对象的创建和 属性变化进行监听

根据类型和作用不同,又可分为监听域对象创建销毁和域对象属性内容变化的

根据监听的域不同,又可以分为监听Request域的,监听Session域的,监听ServletContext域的

在进行Java开发时要遵循三层架构+MVC,Spring操作最核心的就是Spring容器,web层需要注入Service,service层需要注入Dao(Mapper),web层使用Servlet技术充当的话,需要在Servlet中获得Spring容器

1.引入依赖javax.servlet-api

2

AnnotationConfigApplicationContext applicationContext=

new AnnotationConfigApplicationContext(ApplicationContextConfig.class);

AccountService accountService=(AccountService)applicationContext.getBean("accountSezvice");

accountService.transferMoney("tom""lucy",100);

3.

@WebServlet(urlPatterns="/accountServlet")

public class AccountServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception{

 //web层调用service层,获得AccountService,accountservice存在applicationContext中 

    ApplicationContext app = new ClassPathXmlApplicationContext("applicatication.xml");

    AccountService accountService= app.getBean(AccountService.class);

    accountService.transforMoney("tom","lucy",500);

    }

}

在ServletContextListener的contextlnitialized方法中执行ApplicationContext的创建。或在Servlet的init方法中执行ApplicationContext的创建,并给Servlet的load-on-startup属性一个数字值,确保服务器启动Servlet就创建;

将创建好的ApplicationContext存储到ServletContext域中,这样整个web层任何位置就都可以获取到了

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

         version="4.0">

</web-app>

ContextLoaderListener类

(1)创建spring容器

(2)将容器存到ServletContext

public class ContextLoaderListener implements ServletContextListener {

    @Override

    public void contextInitialized(ServletContextEvent sce) {

    //1.创建Spring容器

    ApplicationContext app = new ClassPathXmlApplicationContext("applicatactionContext.xml");

    //2、将容器存储到servletContext域中

    sce.getServletContext().setAttribute("applicationContext",app);

    }

}

AccountServlet类

@WebServlet(urlPatterns="/accountServlet")

public class AccountServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,HttpServletResponse response)throws Exception{

    ServletContext servletContext=request.getServletContext();

    ApplicationContext app=(ApplicationContext)     servletContext.getAttribute("applicationContext.xml"); 



    Accountservice accountservice= app.getBean(AccountService.class);

    accountService.transferMoney("tom","lucy",500);

    }
}

web.xml

<!--配置Listener-->

    <listener>

        <listener-class> com.ting.listener.ContextLoaderListener</listener-class>

    </listener>

避免特殊字符串applicationContext等的出现

web.xml

<!--定义全局参数-->

<context-param>

    <param-name>

        contextConfigLocation

    <param-name>


    <param-value>

        <classpath:applicationContext.xml>

    </param-value>

</context-param>

 public class ContextLoaderListener implements ServletContextListener {

    private String CONTEXT_CONFIG_LOCATION ="contextConfigLocation";


    @Override

    public void contextInitialized(ServletContextEvent sce) {
  

        ServletContext servletContext= sce.getServletContext();


        //0、获取contextConfigLocation配置文件的名称

        String contextConfigLocation= servletContext.getInitParameter(CONTEXT_CONFIG_LOCATION);

        //解析出配置文件名称 
        contextConfigLocation=contextConfigLocation.substring("classpath:".length())

        //1.创建Spring容器

        ApplicationContext app=(ApplicationContext) servletContext.getAttribute(contextConfigLocation);

         //2、将容器存储到servletContext域中

        sce.getServletContext().setAttribute("applicationContext",app);

    }

}

util包

 public class WebApplicationContextUtils {

public static ApplicationContext getWebApplicationContext(ServletContext servletContext){

     ApplicationContext applicationContext=(ApplicationContext)servletContext.getAttribute("applicationContext");

    return applicationContext;

    }

}

改造

protected void doGet (HttpServletRequest request, HttpServletResponse response) throws Exception{

    ServletContext servletCantext= request.getServletContext();

    ApplicationContext app=WebApplicationContextUtils.getWebApplicationContext(servletContext);

    AccountService accountService = app.getBean(AccountService.class);

    accountService.transferMoney("tom","lucy",500);

}

Spring整合web环境

先导入Spring-web的坐标:

<dependency>

    <groupid>org.springframeworkk/groupid>

    <artifactId>spring-web</artifactId>

    <version>5.3.7</version>

</dependency>


 

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

         version="4.0">

<!--定义全局参数-->

<context-param>

    <param-name>

        contextConfigLocation

    <param-name>

    <param-value>

        <classpath:applicationContext.xml>

    </param-value>

</context-param>

<!--配置Listener-->

<listener>

    <listener-class>

        org.springframework.web.context

    </listener-class>

</listener>

</web-app>



@WebServlet(urlPatterns="/accountServlet")

public class AccountServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throw Exception{

    ServletContext servletContext= request.getServletContext(servletContext);

    ApplicationContext app= WebApplicationContextUtils.getWebApplicationContext(servletContext);

    AccountService accountService =app.getBean(AccountService.class);

    accountService.transferMoney("tom", "lucy", 500);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值