Spring中用了哪些设计模式?
创始人
2024-12-26 06:08:15
0

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 {     @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;     } }

相关内容

热门资讯

透视规律!wepoker透视脚... 透视规律!wepoker透视脚本,总是真的是有挂(透视)揭秘教程(有挂揭秘);1、金币登录送、破产送...
透视app!aapoker插件... 透视app!aapoker插件(透视)透视脚本入口(原来真的有挂);1、首先打开aapoker插件最...
透视苹果版"aapo... 透视苹果版"aapoker如何设置胜率"确实真的有挂(透视)教你教程(有挂细节)1、全新机制【aap...
透视数据!wepoker可以开... 透视数据!wepoker可以开透视吗,原本存在有挂(透视)辅助教程(有挂详情);1、在wepoker...
透视软件!aapoker透视脚... 透视软件!aapoker透视脚本下载(透视)ai插件(确实是有挂)aapoker透视脚本下载辅助器中...
透视好友房"hhpo... 透视好友房"hhpoker作弊码"切实是有挂(透视)插件教程(有挂揭秘)1)hhpoker作弊码辅助...
透视辅助!wepoker插件辅... 透视辅助!wepoker插件辅助,本然是有挂(透视)可靠教程(有挂教程);1、下载好wepoker插...
透视透视挂!aapoker怎么... 透视透视挂!aapoker怎么设置提高好牌几率(透视)透视插件(本来是有挂);该软件可以轻松地帮助玩...
透视实锤"hhpok... 透视实锤"hhpoker有后台操作吗"好像真的是有挂(透视)可靠教程(有挂工具)1、hhpoker有...
透视规律!德普之星有透视辅助吗... 透视规律!德普之星有透视辅助吗,原先存在有挂(透视)安装教程(有挂脚本)进入游戏-大厅左侧-新手福利...