zoukankan      html  css  js  c++  java
  • Spring学习随笔(1):为什么要使用Spring


    寒冷的冬天,一周两节课,掏出买了一年没翻过的《Spring实战》。

    刚刚接触spring的我对它还不是很熟悉,对各种知识的认知也比较浅薄,但我会学习的过程通过随笔记录下来,监督自己学下去。

    1 依赖注入(DI)

     大部分的Spring的新手(我)在学习之初对依赖注入这个词感到迷茫,事实上它并没有那么复杂,应用依赖注入会使得代码变得更简单、更容易理解。

    通常,我们开发的java应用都是由多个类组成,它们之间相互协作来完成特定的业务逻辑。每个对象之间相互联系,导致高度耦合的代码。

    参考代码:

    package com.spring;
    
    public class Performer {
        private Violin violin;
        public Performer(){
            violin=new Violin();                //与Violin紧密耦合
        }
        public void play(){
            violin.play();
        }
    }
    class Violin extends Instrument {
    
        public void play() {
            System.out.println("Violin music!");
        }
    }
    class Instrument {
        void play(){};
    }

    上面的代码有个非常明显的问题:Performer在构造函数中创建Violin,这使得Performer与Violin紧密耦合在一起,并且当演奏家需要演奏其他乐器时,就需要改写代码。

    参考代码:

    package com.spring;
    
    public class Performer {
        private Instrument ins;
        public Performer(Instrument ins){
            this.ins=ins;            
        }
        public void play(){
            ins.play();
        }
    }
    class Violin extends Instrument {
    
        public void play() {
            System.out.println("Violin music!");
        }
    }
    class Instrument {
        void play(){};
    }

    不同于之前的演奏家,这次的演奏家没有创建乐器,而是通过构造函数将乐器通过构造参数传入。这便是依赖注入的一种:构造器注入。

    它不仅能够演奏小提琴,无论是钢琴、大提琴、手风琴等继承了Instrument的子类都能作为参赛传入。

    而且它本身并不知道将会演奏什么乐器,这与它无关。这便是依赖注入的好处-------松耦合

    现在performer可以接受任意instrument,那我们如何将instrument传递给它呢(装配)?Spring有多种装配的方式,XML配置是最常用的一种。

    在classpath下创建xml文件:

    <?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="performer" class="com.spring.Performer">
            <constructor-arg ref="violin"/>
        </bean>  
        
        <bean id="violin" class="com.spring.Violin"></bean>  
        
      
    </beans>   

    测试是否成功:

    package com.spring;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class PerformerMain {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext apc = new ClassPathXmlApplicationContext("spring.xml");  
            Performer hello = (Performer) apc.getBean("performer");  
            hello.play();
        }
    
    }


    2 面向切面编程(AOP)

    AOP:允许你把遍布应用各处的功能分离出来形成可重用的组件。

    比方说,系统中的日志、事务管理。安全服务等,通常会分散到你的每一个组件中,哪怕只是调用某个方法,但他依然会使你的代码变得混乱并且不易修改。某个组件应该只关心如何实现自身的业务逻辑,与其无关的代码(日志,安全等)应该少出现甚至不出现。

    AOP:

    AOP使得这些组件具有更高的内聚性以及更加关注与自身业务,完全不需要涉及其他系统服务,甚至你的核心业务根本不知道它们(日志模块,安全模块)的存在。

    为了了解Spring中如何使用切面,我依然使用上面的列子。

    我们现在需要记录每次演奏开始的时间与结束的时间,通常我们会这么做:

    package com.spring;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Performer {
        private Instrument ins;
        private Record rec;
        public Performer(Instrument ins){
            this.ins=ins;            
            this.rec=new Record();
        }
        public void play(){
            rec.starttime();
            ins.play();
            rec.endtime();
        }
    }
    class Record{
        private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public void starttime(){
            System.out.println(df.format(new Date()));
        }
        public void endtime(){
            System.out.println(df.format(new Date()));
        }
    }

    从上面的代码我们可以明显的看出,performer应该专心演奏,而不需要去做记录时间这种事情,这使得Performer的代码复杂化。

    如何将Record抽象为切面呢?只需要在配置文件中声明就可以了:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop" 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
                               http://www.springframework.org/schema/aop
                               http://www.springframework.org/schema/aop/spring-aop.xsd"
         default-autowire="byName">
    
        <bean id="performer" class="com.spring.Performer">
            <constructor-arg ref="violin" />
        </bean>
    
        <bean id="violin" class="com.spring.Violin"></bean>
        <bean id="record" class="com.spring.Record"></bean>
        
        <aop:config>
            <aop:aspect ref="record">
    
                <aop:pointcut expression="execution(* com.spring.Performer.play(..))"  id="play"/>
    
                <aop:before method="starttime" pointcut-ref="play"/>
                <aop:after method="endtime" pointcut-ref="play"/>
            </aop:aspect>
        </aop:config>
    </beans>   
    package com.spring;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Performer {
        private Instrument ins;
        public Performer(Instrument ins){
            this.ins=ins;            //与Violin紧密耦合
        }
        public void play(){
            ins.play();
        }
    }
    class Record{
        private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public void starttime(){
            System.out.println(df.format(new Date()));
        }
        public void endtime(){
            System.out.println(df.format(new Date()));
        }
    }

    运行结果:

    注意:aop除了spring的包外还需要aspectJ框架的包:


  • 相关阅读:
    初始面向对象
    python之路——迭代器和生成器
    函数进阶
    python之路——初识函数
    实现css两端对齐
    http
    background-size
    call和apply的区别
    js兼容性
    面试题
  • 原文地址:https://www.cnblogs.com/zmmi/p/7922186.html
Copyright © 2011-2022 走看看