【体系-单体架构】09-Web架构升级之Spring

本文详细介绍了Spring如何整合Web,重点在于ContextLoaderListener在Web应用中的作用,以及Spring Bean的装配方式,包括scope属性的使用和基于注解与XML配置的区别。此外,还探讨了浏览器端的存储技术,如Cookie、LocalStorage和SessionStorage等。
摘要由CSDN通过智能技术生成

01 Spring整合Web

提示:所有应用程序在启动的时候都有入口,Java SE程序入口是main()方法,Web程序入口在web.xml文件。所以myshop项目中ApplicationContext会出现空指针异常,在Web中应该在web.xml中初始化ApplicationContext。
概述:Spring整合Web其实就是在启动容器时需要自动装载ApplicationContext,其实Spring中会提供ContextLoaderListener自动装配ApplicationContext 的配置信息
说明:spring-context是用在Java SE中得依赖;spring-web才是使用在Java Web中得依赖

<!--向pom.xml文件中追加spring-web依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.17.RELEASE</version>
</dependency>
<!--
	在程序入口web.xml中加载spring-context文件
-->
<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd"
         version="3.1">
    
    <!--
    	classpath 是编译后class路径,即targer\class
    	ContextLoaderListener 是监听器,程序加载时会自动找到<context-parm>节点,然后加载其中得xml配置
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-context*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
/**
 * 将ApplicationContext类交由spring容器进行管理
 * 注意加载顺序
 */
 <?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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值