Spring用到了哪些设计模式?

简介: 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框架进行企业级应用开发。

文章知识点与官方知识档案匹配,可进一步学习相

相关文章
|
1月前
|
设计模式 SQL Java
Spring中的设计模式
Spring中的设计模式
|
5天前
|
设计模式 XML 算法
Spring中的经典的9种设计模式
Spring中的经典的9种设计模式
|
10天前
|
设计模式 网络安全 开发工具
|
1月前
|
设计模式 安全 Java
【初学者慎入】Spring源码中的16种设计模式实现
以上是威哥给大家整理了16种常见的设计模式在 Spring 源码中的运用,学习 Spring 源码成为了 Java 程序员的标配,你还知道Spring 中哪些源码中运用了设计模式,欢迎留言与威哥交流。
|
1月前
|
设计模式 Java 数据库连接
Spring Framework 6 中的设计模式
Spring Framework 6 中的设计模式
28 1
|
1月前
|
设计模式 Java 数据库连接
Spring 中经典的 9 种设计模式
Spring 中经典的 9 种设计模式
59 2
|
1月前
|
设计模式 Java 数据库连接
Spring设计模式(一)
Spring设计模式(一)
|
1月前
|
设计模式 Java 数据库连接
9种设计模式在Spring中的运用
9种设计模式在Spring中的运用
86 0
|
1月前
|
设计模式 Java Spring
Spring5深入浅出篇:Spring工厂设计模式拓展应用
Spring5深入浅出篇:Spring工厂设计模式拓展应用
|
1月前
|
设计模式 Java Spring
Spring5深入浅出篇:Spring与工厂设计模式简介
Spring5深入浅出篇:Spring与工厂设计模式简介