Spring 5 设计模式 - 开始

本文介绍了Spring 5中的关键概念,包括依赖注入如何降低对象耦合,通过构造器注入实现TransferService的灵活性。此外,探讨了Spring的AOP如何处理横切关注点,提供日志、事务等服务。还详细讲述了Spring容器,特别是Bean的生命周期,以及模板设计模式在简化数据访问操作中的应用。
摘要由CSDN通过智能技术生成

Spring 5 设计模式 - 开始

依赖注入

对象之间的依赖增加复杂性,导致对象之间紧耦合。
比如下面的TransferService组件依赖其他两个组件:TransferRepository和AccountRepository:
TransferService

如果直接使用实例:

public class TransferService {
   
    private AccountRepository accountRepository;
    
    public TransferService () {
   
        this.accountRepository = new AccountRepository();
    }
    
    public void transferMoney(Account a, Account b) {
   
        accountRepository.transfer(a, b);
    }
}

TransferService对象需要一个AccountRepository对象,实现账户A到账户B的转账功能。于是,直接增加AccountRepository的实例。这样很难维护,也很难做TransferService的单元测试。

也可以使用工厂模式实现该功能:

public class TransferService {
   
    private AccountRepository accountRepository;
    
    public TransferService() {
   
        this.accountRepository = AccountRepositoryFactory.getInstance("jdbc");
    }
    
    public void transferMoney(Account a, Account b) {
   
        accountRepository.transfer(a, b);
    }
}

上面的代码,我们使用工厂模式增加AccountRepository类的对象。更好的做法是program-to-interface,针对这个具体问题,我们可以增加一个接口,降低耦合:

public interface AccountRepository {
   
    void transfer();
    //other methods
}

我们可以增加JdbcAccountRepositry类,实现AccountRepository接口:

public class JdbcAccountRepositry implements AccountRepositry{
   
    //实现AccountRepositry定义的方法
    // 实现其他方法
}

这样,由工厂类增加的对象很容易维护。使用工厂类,也可以增加可配置的对象。但是,为了降低耦合,获取协作组件的时候在业务组件里增加了工厂类。让我们看看依赖注入怎么解决这个问题。

对于依赖注入模式,我们只是定义依赖性,而不用解决对象之间的依赖。看下图,我们会在需要对象的时候注入他们:

Dependency injection

TransferService依赖AccountRepository和TransferRepository。TransferService可以使用多种TransferRepository完成转账功能,比如可以使用JdbcTransferRepository或者是JpaTransferRepository,依赖哪个,由部署环境决定。

public class TransferServiceImpl implements TransferService {
   
    private TransferRepository transferRepository;
    private AccountRepository accountRepository;
    
    public TransferServiceImpl(TransferRepository transferRepository, AccountRepository accountRepository) {
   
        this.transferRepository = transferRepository;//TransferRepository is injected
        this.accountRepository = accountRepository;//AccountRepository is injected
    }
    public void transferMoney
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值