• Spring笔记1——Spring起源及其核心技术


    Spring的作用

      当我们使用一种技术时,需要思考为什么要使用这门技术。而我们为什么要使用Spring呢?从表面上面SSH这三大框架中,Struts是负责MVC责任的分离,并且提供为Web层提供诸如控制转发、数据校验,类型转换等功能。Hibernate负责将二维数据表抽象成实体类,让我们能够从面向数据表转向面向实体类开发即面向对象开发。而Spring在干什么呢?Spring似乎什么都没干,但好像少了它又不行。Spring究竟是做什么的呢?我们首先从Spring的起源来看。

      提到Spring技术,就不得不提到JavaBean技术,而JavaBean组件技术是Java技术的重要规范。在Java刚刚发布时,大量的程序员因为Applet的强大、独特的功能(能够拥有富客户端的特性,能制作炫目的动画效果)而吸引,但是为JavaBean这门组件技术而留下。JavaBean能够定义软件组件模型,使Java对象能够重用,并能构建复杂系统。但随着技术的发展,提交EJB组件技术,由于EJB技术的庞大,复杂,使很多Java程序员感到很不爽,因此Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中提出Spring框架,Spring的终极目标就是:简化Java开发

      哦,原来Spring是简化Java开发的啊,那Spring究竟采用了什么原则实现的呢?

    Spring四大原则

    1、基于POJO的轻量级和最小侵入性的编程。

    2、通过依赖注入和面向接口实现松耦合。

    3、基于切面和惯例进行声明式编程。

    4、通过切面和模板减少样板式代码。

    Spring的核心技术

      在上面说到Spring的原则,那Spring的核心技术是什么呢?这些技术是怎么做到实现Spring的目标:简化Java开发呢?

      第一种核心技术:依赖注入(Dependency Injection,简称DI),是一个将行为从依赖中分离的技术,简单地说,它允许开发者定义一个方法函数依赖于外部其他各种交互,而不需要编码如何获得这些外部交互的实例。在Spring中即将创建对象的时机交给容器进行处理,无需类在内部创建对象。依赖注入能够使相互协作的软件保存松耦合。 因此在Spring容器中无需自己在类内部实例化具体对象,只需关联对应的接口,然后通过Spring容器注入该接口的实现类的对象即可。

      下面为具体实例:Knight为骑士,BraveKnight为英勇骑士类,骑士有探险的方法(embarkOnQuest)

    正常代码:

    package com.springinaction.knights;
    
    public class BraveKnight implements Knight {
      private SlayDragonQuest quest;      //实例化具体对象
     
      public BraveKnight() {
        this.quest = new SlayDragonQuest();      
      }
     
      public void embarkOnQuest() throws QuestException {
        quest.embark();
      }
     public static void main(String[] args) {
        BraveKnight knight = new BraveKnight();
        knight.embarkOnQuest(); 
      }
    }

    Spring代码:

    package com.springinaction.knights;
    
    public class BraveKnight implements Knight {
      private Quest quest;    //Quest为接口
     
      public BraveKnight(Quest quest) {
        this.quest = quest;       
      }
      public void embarkOnQuest() throws QuestException {
        quest.embark();
      }
    }

    BraveKnight只需依赖Quest接口,无需依赖具体的实现类,这样能够达到松耦合的目的。

    配置文件:

    <?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
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
      < bean id= "knight" class= "com.springinaction.knights.BraveKnight" >
        <constructor-arg ref ="quest" /> <!--构造器注入-->      
      </ bean>
    
      < bean id= "quest"   class= "com.springinaction.knights.SlayDragonQuest"/> 
    </beans>

    运行主方法:

    package com.springinaction.knights;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class KnightMain {
      public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext( "knights.xml"); 
        Knight knight = (Knight) context.getBean( "knight"); 
        knight.embarkOnQuest(); 
      }
    }
    }

      第二种核心技术:面向切面编程(Aspect Oriented Programming,简称AOP)。AOP是将遍布应用各处的功能分类形成可重用的组件。这些分布在各处的任务,包括日志,事物管理和安全等功能组件,相比一些普通的功能模块,这些功能分布在各处,但是如果把这些功能在其他组件中实现,就会使出现代码重复和代码混乱等问题。AOP就是一些通用的功能组合在一起,而我们能把这些功能看成覆盖在很多组件上的一个切面,所以称之为面向切面编程。

    如图所示:常见的事务管理,日志模块,安全模式分布在各个模块,而将它们组件一个切面。

      下面介绍一个实例,来说明AOP,吟游诗人minstrel人需要记录勇士的故事,所以定义一个切面,在勇士出发前赞美勇士,在勇士完成探险后进行歌颂勇士。

    代码:

    package com.springinaction.knights;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class KnightMain {
      public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext( "knights.xml"); 
        Knight knight = (Knight) context.getBean( "knight"); 
        knight.embarkOnQuest(); 
      }
    }
    }

    正常代码:

    package com.springinaction.knights;
    
    public class BraveKnight implements Knight {
        private Quest quest;
        private Minstrel minstrel;
    
        public DamselRescuingKnight(Quest quest, Minstrel minstrel) {
            quest = this.quest;
            minstrel = this.minstrel;
        }
    
        public void embarkOnQuest() throws QuestException {
            minstrel.singBeforeQuest();  //这里BraveKnight类必须依赖minstrel,过于耦合
            quest.embark();
            minstrel.singAfterQuest();
        }
    }
    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-3.0.xsd
               http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
      < bean id= "knight" class= "com.springinaction.knights.BraveKnight" >
        <constructor-arg ref ="quest" />      
      </ bean>
    
      < bean id= "quest"   class= "com.springinaction.knights.SlayDragonQuest" /> 
      < bean id= "minstrel"  class="com.springinaction.knights.Minstrel" />
       
      < aop:config>
        <aop:aspect ref ="minstrel">
          <aop:pointcut id ="embark"
              expression= "execution(* *.embarkOnQuest(..))" /> <!--定义一个切面为embark,执行embarkOnQuest方法->
    
          <aop:before pointcut-ref ="embark"
                      method= "singBeforeQuest"/>    <!--声明前置通知-->
    
          <aop:after pointcut-ref ="embark"
                     method= "singAfterQuest"/>     <!--声明后置通知-->
        </aop:aspect >
      </ aop:config>  
    </beans>

    运行结果:

      第三种技术:模板。Spring提供一些常用模板,模板能够消除样式代码,从而能让你更加关注你自身的职责。

    实例——JDBC代码:

    Image

    使用Spring模板后的代码:

    Imag43e

    总结

      总而言之,Spring提供了依赖注入、面向切面编程,模板等技术,采用面向接口编程思想能够让我们能够使用POJO类即可做到类似EJB的效果,能够简化我们Java的代码,更加关注自己的核心任务。

  • 相关阅读:
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-qrcode
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-volume-up
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-volume-down
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-volume-off
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-headphones
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-flag
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-lock
    【codeforces 750F】New Year and Finding Roots
    【hdu 1517】A Multiplication Game
    json格式化和查看工具
  • 原文地址:https://www.cnblogs.com/codingexperience/p/4517537.html
Copyright © 2020-2023  润新知