Spring and spring boot

 

1. What is Spring Framework

Spring is an open-source Java framework that simplifies enterprise application development by handling infrastructure concerns and allowing developers to focus on business logic. Its key features include IoC (Inversion of Control), DI (Dependency Injection), and modular design.

Spring Framework Modules

  • Spring Core Module: The core component providing the IoC container for managing beans and their dependencies. It includes BeanFactory and ApplicationContext for object creation and dependency injection.
  • Spring AOP Module: Implements Aspect-Oriented Programming to handle cross-cutting concerns like transaction management, logging and monitoring, using aspects defined with the @Aspect annotation.
  • Spring ORM Module: Provides APIs for database interactions using ORM frameworks like JDO, Hibernate and iBatis. It simplifies transaction management and exception handling with DAO support.
  • Spring Web MVC Module: Implements the MVC architecture to create web applications. It separates model and view components, routing requests through the DispatcherServlet to controllers and views.
  • Spring DAO Module: Provides data access support through JDBC, Hibernate or JDO, offering an abstraction layer to simplify database interaction and transaction management.
  • Spring Application Context Module: Builds on the Core module, offering enhanced features like internationalization, validation, event propagation and resource loading via the ApplicationContext interface.


2. Versions Overview:

  • Spring 2.5 (2007): Annotation support
  • Spring 3.0 (2009): Spring Expression Language, profiles
  • Spring 4.0 (2013): Java 8 support, WebSocket module
  • Spring 5.0 (2017): WebFlux, Kotlin support
  • Spring 6.x (2022–2024): Java 17, Jakarta EE 9+, cloud-native enhancement

3. Key features

  • Modular design
  • Dependency Injection
  • Aspect-Oriented Programming (AOP)
  • Transaction management
  • Data access
  • MVC framework
  • Web development
  • Testing support
  • Spring Cloud integration


6. Inversion of Control(IoC)

Transferring dependency management from the application to the container improves scalability and testability.

Inversion of Control (IoC) is a design principle used in object-oriented programming where the control of object creation and dependency management is transferred from the application code to an external framework or container. This reduces the complexity of managing dependencies manually and allows for more modular and flexible code.

IoC Containers:

  • BeanFactory: Basic container for object creation and DI
  • Resource resource = new ClassPathResource("beans.xml");
    BeanFactory factory = new XmlBeanFactory(resource);
    MyBean obj = (MyBean) factory.getBean("myBean");
  • ApplicationContext: Advanced container supporting events, lifecycle management, and resource access
  • ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    MyBean obj = (MyBean) context.getBean("myBean");

7. Dependency Injection(DI)

Injecting dependencies into beans automatically.

Types:

  • Constructor injection: injection using constructor
  • Setter injection: using setter methods
  • Field injection: directly into the fields

9. Spring Beans and scopes

  • Singleton: Single instance per container: If the scope is a singleton, then only one instance of that bean will be instantiated per Spring IoC container and the same instance will be shared for each request. 
  • <beans>
         <!--configure the bean HelloWorld.java
             and declare its scope--> 
         < bean
             id = "hw"
             class= "bean.HelloWorld"
             scope = "singleton" / >
    </beans>
  • Prototype: New instance per request: If the scope is declared prototype, then spring IOC container will create a new instance of that bean every time a request is made for that specific bean.
  •  < beans>
          <!--configure the bean HelloWorld.java
              and declare its scope-->
         < bean
             id = "hw"
             class = "bean.HelloWorld"
             scope = "prototype" / >
    </ beans>
  • Request: New instance per HTTP request
  • Session: New instance per user session

10. Bean LifeCycle:

  • Bean Instantiation: Creation of bean class instance.
  • Bean Post-processing: Use of post-processors for customizing the beans.
  • Bean Initialization: Use of @PostConstruct to set up the beans using methods.
  • Bean Usage: Injection of beans for application-wide use
  • Bean Destruction: Destroys the bean through methods annotated with @PreDestroy

11. Autowiring

Automatic injection of dependencies. Types: no autowiring, by name, by type, constructor.

4. Spring Annotations

Annotations in Spring simplify configuration and setup by providing metadata about our components.


12. What is Spring Boot

Spring Boot simplifies Spring application development by reducing configuration effort and providing auto-configured beans and embedded servers.

Advantages:

  • Minimal configuration
  • Embedded server (Tomcat)
  • Starter POMs for dependencies
  • Rapid development
  • Cloud-friendly

14. Common Spring Boot Annotations

  • @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
  • @Autowired for DI
  • @RestController = @Controller + @ResponseBody
  • @Bean for bean registration

17. Profiles in Spring Boot

In Spring Boot, Profiles allow configuration for applications differently in different environments, such as

  • Development
  • Staging
  • Production
  • Separate configuration files are defined for each profile, which can be activated using environment variables or command-line arguments.

18. Actuator and its usage in Spring Boot

Spring Boot Actuator provides a RESTful API for monitoring and managing Spring Boot applications. These endpoints provide information about applications that can be used to optimize resources and debug issues, including:

  • Environment variables
  • Thread dumps
  • Health checks
  • Metrics
  • Beans
  • Logs
  • 19. Spring AOP and proxy pattern

    • AOP handles cross-cutting concerns (logging, security, transactions)
    • Proxy pattern intercepts method calls to apply aspects dynamically

    20. Key components of AOP

    • Aspect: Bundles cross-cutting concerns
    • Advice: Code executed before/after/around methods
    • Pointcut: Defines join points
    • Join Point: Specific points in execution (method calls, object creation)
    • Weaving: Integrates aspects (compile, load, runtime)

28. Spring MVC and its components

A web framework providing MVC architecture to separate presentation, business logic, and data access layers.

Components:

  • DispatcherServlet: Front controller handling requests
  • Model: Data passed between controller and view
  • View: UI layer
  • Controller: Processes requests and returns responses
DispatcherServlet is the central component of the Spring MVC framework and acts as the front controller, receiving all incoming requests and dispatching them to relevant controllers based on the request URL.

Request Flow:

  • The client sends a request to the DispatcherServlet.
  • DispatcherServlet identifies the appropriate controller based on request mapping.
  • The controller processes the request, interacts with the model, and returns a model object.
  • DispatcherServlet selects the appropriate view based on the returned view name.
  • View renders the model data into the final response and sends it back to the client.

36. Bean Factory vs Application Context

  • Bean Factory: Creates and manages beans.
  • Application Context: Provides additional features like event handling, internationalization, and resource management beyond basic bean management.

HibernateTemplate class: Provides an interface for data access operations like given below, without writing SQL queries.

  • get
  • load
  • save
  • update
  • delete

Explain Spring JDBC API and its classes.

Spring provides a simple way in the form of a JDBC abstraction layer to establish a bridge between database and application. It reduces boilerplate code and configurations.

Key classes:

  • JdbcTemplate: Provides simple methods for executing SQL statements and working with data exchange for applications.
  • DataSource: Establish the connection(bridge) of data exchange from database.
  • SimpleJdbcCall: method present in Spring JDBC API, used for interacting with database-stored procedures.

Fetching records using Spring JdbcTemplate?

Use the query method of JdbcTemplate with the appropriate SQL query and result extractor.

List<User> users = jdbcTemplate.query("SELECT * FROM users", new BeanPropertyRowMapper<>(User.class));




Comments

Popular posts from this blog

Archunit test

Hexagonal Architecture

visitor design pattern