back
2 min read

Spring Framework

·2 min read·backend, java

Spring Framework is a powerful and widely used Java framework for building enterprise applications. It provides comprehensive infrastructure support for developing Java applications, making it easier to manage dependencies, configure components, and build robust, scalable applications.

  1. Dependency Injection

    • Dependency Injection (DI) is a core principle in Spring Framework that helps to achieve loose coupling between components.

    • Instead of objects creating their own dependencies, they are provided with them by an external entity, typically the Spring IoC container.

  2. Types of DI in Spring:

    • Constructor Injection: Dependencies are passed as arguments to the constructor.

    • Setter Injection: Dependencies are set via setter methods.

    • Field Injection: Dependencies are injected directly into private fields using annotations like @Autowired.

  3. Constructor Injection Example

    JAVA
    @Component
    public class UserService {
    
        private final UserRepository userRepository;
        @Autowired // Autowire constructor
        public UserService(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
        // ... methods that use userRepository
    }
    
  4. Setter Injection Example

    JAVA
    @Component
    public class UserService {
    
        private UserRepository userRepository;
    
        @Autowired 
        public void setUserRepository(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    
        // ... methods that use userRepository
    }
    
  5. Field Injection Example

    JAVA
    @Component
    public class UserService {
        @Autowired 
        private UserRepository userRepository;
        // ... methods that use userRepository
    }
    

Spring Modules

Spring Core - Core, Beans, Context, spEL

AOP - Aspect - Instrumentation - Messaging

Data Access/ Integration - ORM, OXM, JMS, JDBC

Web Module - Web, WS, Servlet, Portlet

Spring IOC Container

The Spring IoC container is a framework that automatically injects dependencies into objects, and manages their lifecycle.

It's based on the Inversion of Control (IoC) design principle, which moves the control of object creation, configuration, and management from the application code to the framework.

Understanding IoC Container in Spring Boot with a Real-Time ...

Autowiring in spring

  • Its a feature in spring framework in which the spring container injects the dependencies automatically.

  • Autowiring cannot be used to inject primitive and string values. It works with reference only.

  • Previously, we linked two classes manually in the config.xml file using <ref bean=””>. With autowiring, we can allow the Spring container to handle this process automatically.

  • there are two ways to do autowiring - using XML and @Autowired annotation.