Spring的依赖注入(DI)和面向切面(AOP)代码解析说明

本文介绍了Spring框架中的两个核心概念:依赖注入(DI)和面向切面编程(AOP)。依赖注入通过构造器注入方式降低类之间的耦合度,使代码更易于理解和维护。面向切面编程(AOP)则允许将如日志、事务等通用功能抽取出来,形成可复用的切面,提高代码的内聚性和可维护性。文中通过实例展示了如何在Spring中实现这两个特性。
摘要由CSDN通过智能技术生成

转载自: https://www.cnblogs.com/zmmi/p/7922186.html

 

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框架的包:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值