Spring及其两大核心机制 ioc, aop; xml模板,常用约束

发布于:2023-01-13 ⋅ 阅读:(492) ⋅ 点赞:(0)

二一.概念

 

  • Spring:是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器(框架)。
  • Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式 .
  • 组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:

    • 核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转(IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。
    • Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。
    • Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向切面的编程功能 , 集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理任何支持 AOP的对象。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖组件,就可以将声明性事务管理集成到应用程序中。
    • Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。
    • Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。
    • Spring Web 模块:Web 上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。所以,Spring 框架支持与 Jakarta Struts 的集成。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。
    • Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity、Tiles、iText 和 POI。
  • ioc:控制反转(Inversion of Control)的由来
    •   软件系统在没有引入IOC容器之前,对象A:userServiceImpl依赖于对象B:userDaoImpl,那么对象A在初始化或者运行到某一点的时候,自己必须主动去创建对象B或者使用已经创建的对象B。无论是创建还是使用对象B,控制权都在自己手上

          软件系统在引入IOC容器之后,这种情形就完全改变了,由于IOC容器的加入,对象A与对象B之间失去了直接联系,所以,当对象A运行到需要对象B的时候IOC容器会主动创建一个对象B注入到对象A需要的地方。

          通过前后的对比,我们不难看出来:对象A获得依赖对象B的过程,由主动行为变为了被动行为,控制权颠倒过来了,这就是“控制反转”这个名称的由来。

    • 借助于“第三方”实现具有依赖关系的对象之间的解耦

  • IOC也叫依赖注入(DI)
    • 2004年,Martin Fowler探讨了同一个问题,既然IOC是控制反转,那么到底是“哪些方面的控制被反转了呢?”,经过详细地分析和论证后,他得出了答案:“获得依赖对象的过程被反转了”。控制被反转之后,获得依赖对象的过程由自身管理变为了由IOC容器主动注入。于是,他给“控制反转”取了一个更合适的名字叫做“依赖注入(Dependency Injection)”。他的这个答案,实际上给出了实现IOC的方法:注入。所谓依赖注入,就是由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中。

        所以,依赖注入(DI)和控制反转(IOC)是从不同的角度的描述的同一件事情,就是指通过引入IOC容器,利用依赖关系注入的方式,实现对象之间的解耦。

  • AOP:Aspect oriented programming 面向切面编程

    • AOP 主要用来解决:在不改变原有业务逻辑的情况下,增强横切逻辑代码,根本上解耦合,避免横切逻辑代码重复。

    • AOP 为什么叫面向切面编程

      •  :指的是横切逻辑,原有业务逻辑代码不动,只能操作横切逻辑代码,所以面向横切逻辑
      •  :横切逻辑代码往往要影响的是很多个方法,每个方法如同一个点,多个点构成一个面。这里有一个面的概念

二.通过前面,我们大概已经对spring和IOC,AOP有了一定的了解,那么现在来写一个小案例来帮助理解。

  • 首先我们需要知道如何去找xml核心配置文件的头部约束
    • 去官网找:Spring | Home

       

      • 点击spring framework进入spring框架页面,点击Learn,点击Reference Doc如图:
    • 进来后,点击core,再点击1.2.1 Configuration Metadata后下拉页面,即可看到XML基础配置文件模板:就可以看到


       

    • Core Technologies

       

    • 自己下载保存好,以下是根据上面的步骤从官网copy下来的
      • <?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
                https://www.springframework.org/schema/beans/spring-beans.xsd">
        
            <bean id="..." class="...">  
                <!-- collaborators and configuration for this bean go here -->
            </bean>
        
            <bean id="..." class="...">
                <!-- collaborators and configuration for this bean go here -->
            </bean>
        
            <!-- more bean definitions go here -->
        
        </beans>

  •  然后有了XML基础配置文件模板,后面再根据我们需要的约束,往里面加就好了
  • 以下有几种常用的约束
  • spring-mvc
      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/mvc
        	   http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        </beans>
        
    •  spring-aop

      • <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns:aop="http://www.springframework.org/schema/aop" 
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/aop
        	   http://www.springframework.org/schema/aop/spring-aop.xsd">
        </beans>
        
    • spring-tx,事务处理

      • <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        	xmlns:tx="http://www.springframework.org/schema/tx"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/tx
        	   http://www.springframework.org/schema/tx/spring-tx.xsd">
        </beans>
        
    • spring-context

      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns:context="http://www.springframework.org/schema/context" 
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd">
        </beans>
        
    • spring-task

      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:task="http://www.springframework.org/schema/task"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/task
        	   http://www.springframework.org/schema/task/spring-task.xsd">
        </beans>
        
    • spring-cache

      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:cache="http://www.springframework.org/schema/cache"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/cache
        	   http://www.springframework.org/schema/cache/spring-cache.xsd
        </beans>
        
    • spring-jdbc

      • <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/jdbc
        	   http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        </beans>
        
    • P命名空间

      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:p="http://www.springframework.org/schema/p"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        </beans>
        
    • spring-jee

      • <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns:jee="http://www.springframework.org/schema/jee"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/jee
        	   http://www.springframework.org/schema/jee/spring-jee.xsd    
        </beans>
        
    • spring-jms

      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns:jms="http://www.springframework.org/schema/jms"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/jms
        	   http://www.springframework.org/schema/jms/spring-jms.xsd
        </beans>
        
    • spring-lang

      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns:lang="http://www.springframework.org/schema/lang"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/lang
        	   http://www.springframework.org/schema/lang/spring-lang.xsd    
        </beans>
        
    • spring-oxm

      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns:oxm="http://www.springframework.org/schema/oxm"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/oxm
        	   http://www.springframework.org/schema/oxm/spring-oxm.xsd    
        </beans>
        
    • spring-util

      • 
        <beans xmlns="http://www.springframework.org/schema/beans" 
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns:util="http://www.springframework.org/schema/util"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans
        	   http://www.springframework.org/schema/beans/spring-beans.xsd
        	   http://www.springframework.org/schema/util
        	   http://www.springframework.org/schema/util/spring-util.xsd">
        </beans>
        

    •  总结springmvc常用的
      • <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:p="http://www.springframework.org/schema/p"
               xmlns:mvc="http://www.springframework.org/schema/mvc"
               xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
            <!--这个是扫描,指定包下的组件-->
            <context:component-scan base-package="com.pro"/>
        
            <!--MVC注解驱动,配合handler来使用 -->
            <mvc:annotation-driven/>
            <!--静态资源默认排除在外 要加载静态资源css,js等,就需要写,默认的静态资源在哪处理 ,由Tomcat里面默认的servlet来处理  -->
            <mvc:default-servlet-handler/>
        
            <!-- 配置一个视图解析器,来访问WEB-INF下面的文件
               /WEB-INF/jsp/网页目录及名称 WEB-INF/jsp/index.jsp-->
            <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                <property name="prefix" value="/WEB-INF/jsp/"/>
                <property name="suffix" value=".jsp"/>
        
            </bean>
        </beans>

三.案例练习 

  • 导依赖
    •   <dependencies>
              <!--引入spring jar包,因为spring-webmvc里面其实有我们要的,所以就不再细致导了-->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
                  <version>5.3.19</version>
              </dependency>
          </dependencies>
  • applicationContext.xml文件,可以说是一个工厂/容器,在容器里面创建我们想要控制的对象/或者说类,将要注入的类放进ref,然后在创建工厂对象之后,把他取出来,我们就可以去执行对应的方法,这样就达到了控制反转的效果
    • <?xml version="1.0" encoding="UTF-8"?>
      <!--beans:bean工厂-->
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:p="http://www.springframework.org/schema/p"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              https://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd">
      
          <bean id="userDao" class="com.pro.dao.UserDaoImpl">
              <!--bean表示类,在这里相当于我们在工厂里面创建了这个对象或者说类-->
          </bean>
          <bean id="userDao1" class="com.pro.dao.UserDaoImpl1">
              <!--bean表示类-->
          </bean>
          <bean id="userService" class="com.pro.service.UserServiceImpl">
              <!-- 将ref引用的userDao对象作为参数,传入到setUserDao方法中 -->
              <!-- property表示属性,name表示方法(一定要注意),此处表示com.pro.dao.UserDaoImpl里面的setUserDao方法 -->
              <!--这里可以理解为DI注入,将我们要控制的对象/类注入进去,那么注入userDao1还是userDao,就看你把谁放进ref里了-->
      <!--        <property name="userDao" ref="userDao"/>-->
              <property name="userDao" ref="userDao1"/>
          </bean>
      </beans>
  •  为了更直观,先把测试类放出来给大家看,创建bean工厂对象BeanFactory,去取工厂/或者说容器里面创建对象(根据id取),然后根据ref注入的是谁,将他作为参数传进取出来的这个对象所调用的方法里

 总结:这种通过xml文件去控制反转的写法,可以更好的帮助理解,但是以后的开发当中,很多步骤都已经写好了,我们只需要通过注解的方式去写就好了。


网站公告

今日签到

点亮在社区的每一天
去签到