Spring14:Spring整合MVC框架

1.搭建web环境

相关的jar包坐标:额外引入web的相关jar包

<dependencies>
    <!--引入web相关的jar包
    provide仅仅需要在编译和测试阶段,同样provide将不会被打包到lib目录下。
    -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.1</version>
      <scope>provided</scope>
    </dependency>
    <!--Spring对web支持的jar包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <!--Spring-core-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <!--Spring-beans-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <!--添加mysql驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
    <!--mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>
    <!--jdbc-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <!--Spring整合mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.2</version>
    </dependency>
    <!--druid连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.18</version>
    </dependency>
    <!--Spring5.x-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <!--日志-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.25</version>
    </dependency>
    <!--测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--Spring动态代理-->
    <!--解析切入点表达式-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.6</version>
    </dependency>
  </dependencies>

2.为什么要整合MVC框架

1.MVC框架提供了控制器(Controller)调用Service
2.处理请求、响应
3.接收请求参数:好比servlet的request.getParameter
4.控制程序的运行流程
5.视图解析:解析成JSP、JSON、Freemarker、Thyemeleaf返回给客户端

3.Spring整合MVC框架的核心思路

3.1准备工厂

1.web开发过程中,如何创建工厂呢?

//核心代码还是这句:
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");

//但是要换一个适合web开发的工厂:WebXmlApplicationContext
ApplicationContext ctx = new WebXmlApplicationContext();

2.如何保证工厂唯一且可以共用:工厂是一个重量级资源:能被共用,线程安全,且一个应用只能创建一次。

  • 如何保证工厂可以被共用:

    web开发设计到的几个作用域:request<session<ServletContext(application)

    ServletContext这个作用域(对象),每个应用都只有唯一一个,作用域覆盖整个应用,所以将Spring的工厂对象存到ServletContext中,那么就可以被大家共用了。

    底层代码:ServletContext.setAttribute("xxxx",ctx);

  • 如何保证工厂对象唯一:如何保证创建工厂的代码只会被调用一次

    因为ServletContext这个对象是整个应用唯一的,那么ServletContext这个对象的创建只会创建一次,我们随着ServletContext的创建然后创建工厂WebXmlApplicationContext,那么就可以保证每个应用中只有一个工厂,也就保证了工厂的唯一性。

  • 进一步:我如何知道ServletContext对象什么时候创建呢?

    要借助ServletContextListener这个对象帮我们监听完成,ServletContextListener会帮我们监听ServletContext对象的创建。只要ServletContext对象创建了,就会被ServletContextListener监听到,然后我们就可以创建工厂对象了。

    所以可以在监听器ServletContextListener中执行创建工厂的代码,也就可以保证创建工厂的代码只会执行一次。

3.准备工厂小结

 //ServletContextListener中执行创建工厂的代码(保证工厂唯一)
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
//同时将工厂存到ServletContext的属性中
ServletContext.setAttribute("xxx",ctx) (共用)

4.Spring将上面的两步封装成了一个ContextLoaderListener类,直接用这个类即可。

	 1. 创建工厂
	 2. 把工厂存在ServletContext中

5.观察源码:

在这里插入图片描述
6.ContextLoaderListener使用方式

它既然是个ServeletContextListener(继承了ServeletContextListener),那么只需要在web.xml中配置即可。

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

配置之后,就可以使用这个类了,之后ContextLoaderListener就会帮我们创建工厂,进而将工厂存到ServeletContext中作为一个属性。

注意一个细节:我们在创建工厂的时候,需要指定一个applicationContext.xml的配置文件。所以在web.xml中指定applicationContext.xml配置文件的位置。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

7.代码整合:

将Service作为属性注入到Controller中。

4.Spring开发过程中多配置文件的处理

1.Spring会根据需要,把配置信息分门别类的放置在多个配置文件中,便于后续的管理及维护。

DAO  ------  applicationContext-dao.xml 
Service ---  applicationContext-service.xml
Action  ---  applicationContext-action.xml

注意:虽然提供了多个配置文件,但是后续应用的过程中,还要进行整合

2.整合多配置文件

  • 通配符方式
    1. 非web环境
       ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext-*.xml");
    
    2. web环境:web.xml
       <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:applicationContext-*.xml</param-value>
       <context-param>
    
  • < import resource=/ >标签
    提供一个主配置文件:applicationContext.xml,不放具体的配置内容,只为了整合其他配置内容,主要就是import 标签
        <import resource="applicationContext-dao.xml " />
        <import resource="applicationContext-service.xml " />
        <import resource="applicationContext-action.xml " />
        
    1. 非web环境
       ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
    2. web环境
       <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:applicationContext.xml</param-value>
       <context-param>
    

猜你喜欢

转载自blog.csdn.net/tttxxl/article/details/115532428
今日推荐