Spring用到了哪些设计模式?

个人主页: 进朱者赤

阿里非典型程序员一枚 ,记录平平无奇程序员在大厂的打怪升级之路。 一起学习Java、大数据、数据结构算法(公众号同名

引言

Spring框架作为Java企业级应用开发的领军框架之一,广泛采用了各种设计模式以提升框架的灵活性、可扩展性和可维护性。本文旨在深入剖析Spring框架中运用的主要设计模式,并通过代码示例来论证这些设计模式的实际应用。

一、单例模式(Singleton Pattern)

在Spring中,默认情况下,通过容器管理的Bean是单例的。以下是一个简单的Spring配置示例,展示了如何定义一个单例Bean:

<beans>
    <bean id="mySingletonBean" class="com.example.MySingletonClass" scope="singleton"/>
</beans>

在上面的配置中,scope="singleton"指定了Bean的作用域为单例。这意味着每次从Spring容器中请求mySingletonBean时,都会返回同一个实例。

二、工厂模式(Factory Pattern)

Spring容器本身就是一个Bean工厂,负责创建和管理Bean实例。以下是使用Spring容器作为工厂来获取Bean的示例:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyService myService = (MyService) context.getBean("myServiceBean");
        myService.doSomething();
    }
}

在上述代码中,ClassPathXmlApplicationContext是Spring提供的一个应用上下文实现,它负责加载配置文件并初始化Bean。通过调用getBean方法,我们可以从容器中获取指定的Bean实例。

三、代理模式(Proxy Pattern)

Spring AOP功能通过代理模式实现。以下是一个使用Spring AOP的示例,展示了如何为方法调用添加前置和后置通知:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.MyService.doSomething(..))")
    public void beforeDoSomething() {
        System.out.println("Before doSomething method call");
    }

    @After("execution(* com.example.MyService.doSomething(..))")
    public void afterDoSomething() {
        System.out.println("After doSomething method call");
    }
}

在上述代码中,LoggingAspect是一个切面类,它定义了两个通知方法:beforeDoSomethingafterDoSomething。通过注解配置,Spring会在MyService类的doSomething方法调用前后分别执行这两个通知方法。实际上,Spring会为MyService类的实例创建一个代理对象,并在代理对象中嵌入切面逻辑。

四、模板方法模式(Template Method Pattern)

Spring的JdbcTemplate是模板方法模式的一个典型应用。以下是使用JdbcTemplate执行查询操作的示例:

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class MyDao {
    private JdbcTemplate jdbcTemplate;

    public MyDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<MyObject> findObjects() {
        String sql = "SELECT * FROM my_table";
        return jdbcTemplate.query(sql, new RowMapper<MyObject>() {
            @Override
            public MyObject mapRow(ResultSet rs, int rowNum) throws SQLException {
                MyObject obj = new MyObject();
                obj.setId(rs.getLong("id"));
                obj.setName(rs.getString("name"));
                // ... set other properties
                return obj;
            }
        });
    }
}

在上述代码中,JdbcTemplate提供了query方法作为模板方法的骨架,而具体的行映射逻辑(即如何将结果集的每一行映射为对象)则由RowMapper接口的实现类提供。这样,用户只需要关注业务逻辑的实现(即如何映射数据),而无需关心底层数据库操作的细节。

小结

总结来说,Spring框架通过巧妙地运用各种设计模式,实现了高内聚、低耦合的代码结构,提高了框架的灵活性和可扩展性。通过深入理解这些设计模式在Spring中的应用,并结合具体的代码示例进行分析,我们可以更好地掌握Spring的精髓和设计思想,从而更好地利用Spring框架进行企业级应用开发。
在这里插入图片描述

  • 36
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进朱者赤

多多支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值