问题描述
弹簧框架中使用了哪些设计模式?
推荐答案
使用了许多不同的设计模式,但是有一些明显的模式:
-
singleton-弹簧配置文件中定义的bean默认情况下是单例.
-
模板方法 - 广泛用于处理样板重复代码(例如,干净的连接等).例如, jdbctemplate =" http://static.springframework.org/spring/docs/2.5.x/reference/jms.htms.html#jms-jmstemplate" rel =" noreferrer"> jmstemplate , jpatemplate .
更新以下注释:对于MVC,您可能需要阅读 MVC参考
MVC中使用的一些明显模式:
-
模型视图 :-). Spring MVC的优势在于,您的控制器是Pojos,而不是Servlet.这使控制器的测试更加容易.要注意的一件事是,控制器仅返回逻辑视图名称,并且视图选择留给单独的 ViewResolver .这使重复使用不同视图技术的控制器变得更容易.
-
前控制器. Spring提供 dispatcherservlet "> dispatcherservlet dispatcherservlet a>确保将传入的请求派发给您的控制器.
其他推荐答案
当然还有依赖注入或IOC(控制倒置),这是整个BeanFactory/ApplicationContext东西的核心.
其他推荐答案
di事物实际上是某种策略模式.每当您想成为可以交换的逻辑/实现时,通常都会在主机类上找到一个接口和适当的固定器方法来接线该接口的自定义实现.
问题描述
What design patterns are used in Spring framework?
推荐答案
There are loads of different design patterns used, but there are a few obvious ones:
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.
Update following comments: For MVC, you might want to read the MVC Reference
Some obvious patterns in use in 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 DispatcherServlet to 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.
其他推荐答案
And of course dependency injection, or IoC (inversion of control), which is central to the whole BeanFactory/ApplicationContext stuff.
其他推荐答案
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.