spring Spring框架中使用了哪些设计模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/755563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:51:13  来源:igfitidea点击:

What design patterns are used in Spring framework?

design-patternsspring

提问by Tony

What design patterns are used in Spring framework?

Spring框架中使用了哪些设计模式?

采纳答案by toolkit

There are loads of different design patterns used, but there are a few obvious ones:

使用了大量不同的设计模式,但有一些明显的:

  • Proxy - used heavily in AOP, and remoting.

  • Singleton - beans defined in spring config files are singletons by default.

  • Template method - used extensively to deal with boilerplate repeated code (such as closing connections cleanly, etc..). For example JdbcTemplate, JmsTemplate, JpaTemplate.

  • 代理 - 在AOP远程处理中大量使用。

  • 单例 - 在 spring 配置文件中定义的 bean 默认情况下是单例。

  • 模板方法 - 广泛用于处理样板重复代码(例如干净地关闭连接等)。例如JdbcTemplateJmsTemplateJpaTemplate



Update following comments: For MVC, you might want to read the MVC Reference

更新以下评论:对于 MVC,您可能需要阅读MVC 参考

Some obvious patterns in use in MVC:

MVC 中使用的一些明显模式:

  • Model View Controller:-) . The advantage with Spring MVC is that your controllers are POJOs as opposed to being servlets. This makes for easier testing of controllers. One thing to note is that the controller is only required to return a logical view name, and the view selection is left to a separate ViewResolver. This makes it easier to reuse controllers for different view technologies.

  • Front Controller. Spring provides DispatcherServletto ensure an incoming request gets dispatched to your controllers.

  • View Helper- Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views.

  • 模型视图控制器:-) 。Spring MVC 的优点是您的控制器是 POJO,而不是 servlet。这使得控制器的测试更容易。需要注意的一件事是控制器只需要返回一个逻辑视图名称,视图选择留给单独的ViewResolver。这使得为​​不同的视图技术重用控制器变得更容易。

  • 前端控制器。Spring 提供DispatcherServlet来确保传入的请求被分派到您的控制器。

  • 视图助手- Spring 有许多自定义 JSP 标记和速度宏,以帮助将代码与视图中的表示分离。

回答by Chochos

And of course dependency injection, or IoC (inversion of control), which is central to the whole BeanFactory/ApplicationContext stuff.

当然还有依赖注入或 IoC(控制反转),这是整个 BeanFactory/ApplicationContext 的核心。

回答by Oliver Drotbohm

The DI thing actually is some kind of strategy pattern. Whenever you want to be some logic/implementation exchangeable you typically find an interface and an appropriate setter method on the host class to wire your custom implementation of that interface.

DI 的东西实际上是某种策略模式。每当您希望某些逻辑/实现可交换时,您通常会在宿主类上找到一个接口和一个适当的 setter 方法来连接该接口的自定义实现。

回答by skaffman

Spring is a collection of best-practise API patterns, you can write up a shopping list of them as long as your arm. The way that the API is designed encourages you (but doesn't force you) to follow these patterns, and half the time you follow them without knowing you are doing so.

Spring 是最佳实践 API 模式的集合,只要你有手臂,你就可以写下它们的购物清单。API 的设计方式鼓励您(但不强迫您)遵循这些模式,并且有一半的时间您在不知道自己正在这样做的情况下遵循它们。

回答by Suman Mitra

Service Locator Pattern - ServiceLocatorFactoryBean keeps information of all the beans in the context. When client code asks for a service (bean) using name, it simply locates that bean in the context and returns it. Client code does not need to write spring related code to locate a bean.

服务定位器模式 - ServiceLocatorFactoryBean 保存上下文中所有 bean 的信息。当客户端代码使用名称请求服务(bean)时,它只是在上下文中定位该 bean 并返回它。客户端代码不需要编写spring相关的代码来定位一个bean。

回答by ksevindik

Observer-Observable: it is used in ApplicationContext's event mechanism

Observer-Observable:在ApplicationContext的事件机制中使用

回答by Gurpreet

Factory pattern is also used for loading beans through BeanFactory and Application context.

工厂模式也用于通过 BeanFactory 和 Application 上下文加载 bean。

回答by Praveen Sharma

Factory Method patter: BeanFactory for creating instance of an object Singleton : instance type can be singleton for a context Prototype : instance type can be prototype. Builder pattern: you can also define a method in a class who will be responsible for creating complex instance.

工厂方法模式:用于创建对象实例的 BeanFactory 单例:实例类型可以是上下文的单例原型:实例类型可以是原型。构建器模式:您还可以在类中定义一个方法,该方法将负责创建复杂实例。

回答by jsr

Spring container generates bean objects depending on the bean scope (singleton, prototype etc..). So this looks like implementing Abstract Factory pattern. In the Spring's internal implementation, I am sure each scope should be tied to specific factory kind class.

Spring 容器根据 bean 范围(单例、原型等)生成 bean 对象。所以这看起来像是实现了抽象工厂模式。在 Spring 的内部实现中,我确信每个范围都应该绑定到特定的工厂种类类。