Spring中用了哪些设计模式?

Spring框架广泛应用了多种设计模式,以实现其灵活性、可扩展性和可维护性。以下是一些在Spring中常见的设计模式及其应用示例:

1. 单例模式(Singleton Pattern)
Spring中的Bean默认都是单例的,这意味着在整个应用上下文中,每个Bean定义只有一个实例。

@Service
public class SingletonService {
    // 单例Bean的实现
}

2. 工厂模式(Factory Pattern)
Spring使用BeanFactory和ApplicationContext接口来创建和管理Bean实例,这是工厂模式的典型应用。

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyService myService = context.getBean(MyService.class);

3. 代理模式(Proxy Pattern)
Spring AOP(面向切面编程)通过代理模式实现,可以在不修改原有代码的情况下增强功能。

@Aspect
@Component
public class LoggingAspect {
    @Before("execution(* com.example.service.*.*(..))")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("Executing: " + joinPoint.getSignature().getName());
    }
}

4. 模板方法模式(Template Method Pattern)
Spring提供了多种模板类,如JdbcTemplate、RestTemplate等,这些模板类定义了一套固定的流程,但允许子类重写某些步骤。

public class JdbcCustomerDao {
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public Customer getCustomerById(int id) {
        return jdbcTemplate.queryForObject("SELECT * FROM customers WHERE id = ?", new Object[]{id}, new CustomerRowMapper());
    }
}

5. 观察者模式(Observer Pattern)
Spring的事件机制基于观察者模式,允许应用组件监听和响应特定事件。

public class CustomApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("Application context has been initialized or refreshed.");
    }
}

6. 策略模式(Strategy Pattern)
Spring的依赖注入机制允许开发者灵活地选择和切换不同的实现策略。

public interface PaymentStrategy {
    void pay(int amount);
}

@Service
public class CreditCardPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid with credit card: " + amount);
    }
}

@Service
public class PayPalPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid with PayPal: " + amount);
    }
}

7. 装饰器模式(Decorator Pattern)
Spring Security中的认证和授权机制使用了装饰器模式,通过层层包装来增强功能。

public class CustomAuthenticationProvider implements AuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // 自定义认证逻辑
        return authentication;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }
}

8. 适配器模式(Adapter Pattern)
Spring MVC中的HandlerAdapter接口允许不同的控制器实现以统一的方式被调用,这是适配器模式的体现。

public class SimpleControllerHandlerAdapter implements HandlerAdapter {
    @Override
    public boolean supports(Object handler) {
        return (handler instanceof Controller);
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return ((Controller) handler).handleRequest(request, response);
    }

    @Override
    public long getLastModified(HttpServletRequest request, Object handler) {
        return -1;
    }
}

相关推荐

  1. Spring哪些设计模式

    2024-07-19 18:46:04       18 阅读
  2. Spring整理-Spring框架哪些设计模式

    2024-07-19 18:46:04       53 阅读
  3. Spring 框架哪些设计模式

    2024-07-19 18:46:04       70 阅读
  4. spring框架哪些设计模式

    2024-07-19 18:46:04       54 阅读
  5. Spring 框架哪些设计模式?

    2024-07-19 18:46:04       37 阅读
  6. 65.Spring 框架哪些设计模式

    2024-07-19 18:46:04       54 阅读
  7. 每日一题:Spring 框架哪些设计模式

    2024-07-19 18:46:04       30 阅读
  8. 在前端开发哪些设计模式

    2024-07-19 18:46:04       35 阅读
  9. JDK哪些设计模式

    2024-07-19 18:46:04       153 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 18:46:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 18:46:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 18:46:04       58 阅读
  4. Python语言-面向对象

    2024-07-19 18:46:04       69 阅读

热门阅读

  1. 常见日志报错及解决方法

    2024-07-19 18:46:04       18 阅读
  2. OpenSNN推文:目前在哪些方面AI能完全取代人类

    2024-07-19 18:46:04       16 阅读
  3. 面试问题:React基本概念,和所遇到的CPU和IO问题

    2024-07-19 18:46:04       19 阅读
  4. Linux处理文件sed

    2024-07-19 18:46:04       19 阅读
  5. Hadoop基础组件介绍!

    2024-07-19 18:46:04       19 阅读
  6. Android 14 适配之 - 全屏 intent 通知

    2024-07-19 18:46:04       19 阅读
  7. Vector容器笔记

    2024-07-19 18:46:04       19 阅读
  8. HashMap的putVal方法(源码分析)

    2024-07-19 18:46:04       24 阅读
  9. 周报 | 24.7.8-24.7.14文章汇总

    2024-07-19 18:46:04       23 阅读
  10. C语言习题~day35

    2024-07-19 18:46:04       24 阅读
  11. Python打包exe 以及压缩大小方法

    2024-07-19 18:46:04       21 阅读
  12. 【ELK】简述

    2024-07-19 18:46:04       20 阅读
  13. 架构以及架构中的组件

    2024-07-19 18:46:04       17 阅读