⬆️ ⬇️

Design Patterns Used in the Spring Framework

Hi, habrovchane! The long weekend is over, which means that it's time to share a new useful translation. Today let's talk about the design patterns used in the Spring Framework. As you guessed, this material is timed to the start of the recruitment of a new group in the course "Developer in the Spring Framework" , which starts on May 28. Let's start.





In this article, we will review several design patterns that are widely used in the Spring Framework. Design patterns describe programming techniques in object-oriented software development.



Here are some of the famous patterns used in the Spring Framework:





Proxy (Alternate)



The proxy pattern is widely used in AOP and remoting .



A good example of using Proxy is org.springframework.aop.framework.ProxyFactoryBean .



This factory creates AOP proxies based on Spring beans.



A proxy provides a surrogate for another object to control access to it.



Read more about Proxy here .



Singleton



The Singleton pattern guarantees that only one instance of the object will exist in memory that will provide services.



In Spring, the scope of a bean (scope) is by default equal to singleton and the IoC container creates exactly one instance of an object per Spring IoC container.



The spring container will store this single instance in the singleton cache, and all subsequent requests and references for this bean will get the cached object.



It is recommended to use the singleton scope for stateless bins.



The scope of a bean can be defined as singleton or prototype (a new instance is created with each request for a bean).



Configuration example in xml file:



 <!--      singleton --> <bean id = "..." class = "..." scope = "singleton/prototype"> <!--    --> </bean> 


Read more about Singleton here .



Factory (Factory)



This pattern allows you to initialize an object through a public static method called a factory method.



Spring uses the Factory pattern to create a bean object using the following two approaches.





Another container present in Spring that adds enterprise-specific features. These functions include the ability to read parameters from property files and publish application events for event listeners.



To work with this container, the org.springframework.context.ApplicationContext interface is org.springframework.context.ApplicationContext .



The following are the most commonly used implementations of ApplicationContext .





Examples:



 package com.eduonix.springframework.applicationcontext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "C:/work/IOC Containers/springframework.applicationcontext/src/main/resources/bean-factory-config.xml"); HelloApplicationContext obj = (HelloApplicationContext) context.getBean("helloApplicationContext"); obj.getMsg(); } } 


To better understand this, let's look at a real-world example. First, the configuration:



 <bean id="welcomerBean" class="com.mysite.Welcomer" factory-method="createWelcomer"> <constructor-arg ref="messagesLocator"></constructor-arg> </bean> <bean id="messagesLocator" class="com.mysite.MessageLocator"> <property name="messages" value="messages_file.properties"></property> </bean> 


And now bin:



 public class Welcomer { private String message; public Welcomer(String message) { this.message = message; } public static Welcomer createWelcomer(MessageLocator messagesLocator) { Calendar cal = Calendar.getInstance(); String msgKey = "welcome.pm"; if (cal.get(Calendar.AM_PM) == Calendar.AM) { msgKey = "welcome.am"; } return new Welcomer(messagesLocator.getMessageByKey(msgKey)); } } 


You can read more about Factory here .



Template



This pattern is widely used to work with recurring boilerplate code (such as closing connections, etc.).

For example, JdbcTemplate , JmsTemplate , JpaTemplate ( Translator's Note: JpaTemplate is deprecated with Spring 3.1 ).



You can read more about Template here .



Model View Controller (Model-View-Controller)



The advantage of Spring MVC is that your controllers are POJOs, not servlets. This makes testing controllers easier. It is worth noting that the controllers are only required to return the logical name of the view, and the choice of the view remains with the ViewResolver . This makes it easy to reuse controllers with different presentation options.



Read more about Model View Controller here .



Front Controller (Query Controller)



Spring provides the DispatcherServlet to ensure that an incoming request is sent to your controllers.



The Front Controller pattern is used to provide a centralized request processing mechanism, so that all requests are processed by a single handler. This handler can authenticate, authorize, register or track the request, and then transfer the request to the appropriate controller.



Read more about Front Controller here .



View Helper (View Subsidiary)



There are several custom JSP tags and velocity macros in Spring to help separate code from presentation.



View Helper separates static content in a view, such as JSP, from business logic processing.



Frameworks, such as Spring and Struts, provide their own tag libraries for encapsulating processing logic in helpers instead of placing logic in a view, such as JSP files.



Read more about View Helper here .



Dependency injection and inversion of control (IOC) (dependency injection and inversion control)



The IoC container in Spring is responsible for creating an object, linking objects together, configuring objects, and processing their entire life cycle from creation to complete destruction.



In the Spring container, Dependency Injection (DI) is used to manage application components. These components are called "Spring beans" (Spring Beans).



Read more about Dependency Injection here .



Service Locator



ServiceLocatorFactoryBean stores information about all bins in context. When client code requests a service (bin) by name, it simply finds this component in context and returns it. Client code does not need to write Spring-related code to find a bin.



The Service Locator pattern is used when we want to find various services using JNDI. Given the high cost of finding services in JNDI, the Service Locator uses caching. When a service is requested for the first time, Service Locator searches for it in JNDI and caches the object. Further search for the same service through the Service Locator is performed in the cache, which significantly improves the application performance.



Read more about the Service Locator here .



Observer-Observable (Observer)



Used in the ApplicationContext event mechanism.

Defines a one-to-many relationship between objects, so that when a state of a single object changes, all its subscribers are automatically notified and updated.



You can read more about Observer here .



Context Object (Context Object)



The Context Object pattern encapsulates system data in a context object for sharing by other parts of the application without binding the application to a specific protocol.



ApplicationContext is the central interface in a Spring application for providing application configuration information.



You can read more about Context Object here .



Links



https://stackoverflow.com/questions/755563/what-design-patterns-are-used-in-spring-framework



This is the material that came out. We are waiting for your comments and invite you to a free webinar , in the framework of which we will develop a full-fledged classic Web application with UI, authentication and authorization. We also study the features of Web applications and try to solve some of the Enterprise tasks that we have. Webinar will be held today.



')

Source: https://habr.com/ru/post/451516/



All Articles