📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Here is the list of well-known design patterns used in the Spring Framework.
Proxy Design Pattern
A good example of a proxy design pattern is org.springframework.aop.framework.ProxyFactoryBean. This factory constructs an AOP proxy based on Spring beans. The proxy provides a surrogate or placeholder for another object to control access to it.
Read more details about Proxy Design Pattern here at Proxy Design Pattern.
Singleton Design Pattern
To define a bean as a singleton in XML, we would write, for example:
<bean id="accountService" class="com.foo.DefaultAccountService"/>
<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
@Configuration
public class AppConfiguration {
@Bean
@Scope("singleton") // default scope
public UserService userService(){
return new UserService();
}
}
Factory design pattern
Spring BeanFactory Container: It is the simplest container present in the spring framework which provides the basic support for DI (Dependency Injection). We use the following interface to work with this container.
Spring ApplicationContext Container: It is another container present in the spring container that adds extra enterprise-specific functionality. These functionalities include the capability to resolve textual messages from a properties file and publish application events to attentive event listeners. We use the following interface to work with this container.
ApplicationContext Example:
package net.javaguides.spring.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
context.close();
}
}
Read more details about the Factory design pattern here at a Factory design pattern.
Template Design Pattern
Read more details about Template Design Pattern here at Template Design Pattern.
Command Pattern
Spring's JdbcTemplate uses the Command pattern, where SQL statements are encapsulated as objects, providing a common interface for executing them.
Chain of Responsibility Pattern
Spring's filter chains, like the ones used in Spring Security, employ the Chain of Responsibility pattern, where a request is passed along a chain of handlers.
Builder Pattern
Spring's BeanFactory and ApplicationContext use the builder pattern to create complex objects with different configurations.
Model View Controller Pattern
Read more details about Model View Controller here at Model View Controller Pattern.
Front Controller Pattern
Spring provides DispatcherServlet to ensure an incoming request gets dispatched to your controllers.The front controller design pattern is used to provide a centralized request-handling mechanism so that all requests will be handled by a single handler. This handler can do the authentication/ authorization/ logging or tracking of requests and then pass the requests to corresponding handlers.
Read more details about Front Controller Pattern here at Front Controller Pattern.
View Helper Pattern
View Helper Pattern separates the static view such as JSPs from the processing of the business model data.
Frameworks like Spring and Struts provide their own tag libraries to encapsulate processing logic in a helper instead of a view such as JSP files.
Read more details about View Helper Pattern here at View Helper Pattern.
Dependency Injection (DI) Pattern
Read more details about the Dependency injection Pattern here at Dependency injection Pattern.
Service Locator Pattern
The service locator design pattern is used when we want to locate various services using JNDI lookup. Considering the high cost of looking up JNDI for a service, the Service Locator pattern makes use of the caching technique. For the first time, a service is required, Service Locator looks up in JNDI and caches the service object. Further lookup or the same service via Service Locator is done in its cache which improves the performance of the application to a great extent.
Read more details about Service Locator Pattern here at Service Locator Pattern.
Observer Pattern
Read more details about Observer Design Pattern here at Observer Design Pattern.
Context Object Pattern
The ApplicationContext is the central interface within a Spring application for providing configuration information to the application.
Read more details about the Context Object Pattern here at Context Object Pattern.
Comments
Post a Comment
Leave Comment