API&M Unit-01
Click on any image for better view!

1) Advantages of Spring

  1. Spring is everywhere
    • Spring is everywhere and is trusted globally
    • Popular companies like Amazon, Google, and Microsoft use Spring
    • It supports everything from simple applications to complex applications
  2. Spring is Flexible
    • Spring provides flexible libraries that support any type of application
    • It includes
      • Inversion of Control (IoC)
      • Dependency Injection (DI)
  3. Spring is Productive
    • Spring adds powerful tools that increase productivity
    • Spring helps to build microservices quickly
  4. Spring is Fast
    • Spring allows applications to start, run and shut down quickly
  5. Spring is secure
    • Spring keeps the data safe from security vulnerabilities
    • Modern applications are safer with Spring Security
  6. Spring is supportive
    • Spring supports developers by providing
      • training
      • certificates, etc...
API&M Unit-01

2) Spring Framework

Architecture

  1. Data Access/Integration
    • Spring allows developers to easily access the data from databases
    • It includes modules like
      • JDBC
      • ORM
      • OXM
      • JMS
      • transaction management
  2. Web
    • It supports web-oriented features like Spring-MVC, RESTful services.
    • It includes modules like
      • web
      • servlet
      • portlet
      • struts
  3. AOP and Instrumentation
    • AOP stands for Aspect-Oriented Programming
    • Spring separates logging and security from the main program
  4. Core Container
    • It mainly focuses on two principles
      1. Inversion of Control (IoC)
      2. Dependency Injection (DI)
    • It includes modules like
      • Beans
      • Core
      • Context
      • Expression Language
  5. Test
    • Spring tests the applications by using tools like
      • JUnit
      • TestNG
API&M Unit-01

3) Configuration in IoC Container

  1. @configuration
    • It is used on classes that contain bean definitions
  2. @Bean
    • It is used on methods that define the bean

Syntax

@Configuration
public class A {
  @Bean
  public B fun() {
     ...
  }
}
        

Inter Bean Dependencies

Syntax

@Configuration
public class A {
  @Bean
  public B fun1() {
     ...
  }
  @Bean
  public C fun2() {
     ...
  }
}
            

Program

A.java

@Component
public class A {
  public void display() {
    System.out.println("Hello World");
  }
}
            

B.java

@Configuration
public class B{
  @Bean
  public A fun1(){
    return new A();
  }
}
            

Main.java

public class Main {
  public static void main(String[] args){
    ApplicationContext context = new AnnotationConfigApplicationContext(B.class);
    A obj = context.getBean(A.class);
    obj.display();
  }
}
            

Output

Hello World
            
API&M Unit-01

4) Dependency Injection

  1. Constructor Injection
    • Constructor Injection is a type of Dependency Injection
    • Constructor Injection is a technique where dependencies are injected into the class by using a constructor
    • It calls the constructor with required dependencies

    Syntax

    @Component
    public class B {
      @Autowired
      public B(public A a) {
        this.a = a;
      }
    }
                    

    Program

    A.java

    @Component
    public class A {
      public void display() {
        System.out.println("Hello World");
      }
    }
                        

    B.java

    @Component
    public class B{
      @Autowired
      public B(public A a){
        this.a = a;
      }
    }
                        

    Main.java

    public class Main {
      public static void main(String[] args){
        ApplicationContext context = new AnnotationConfigApplicationContext(B.class);
        A obj = context.getBean(A.class);
        obj.display();
      }
    }
                        

    Output

    Hello World
                        
  2. Setter Injection
    • Setter Injection is a type of Dependency Injection
    • Setter Injection is a technique where dependencies are injected into the beans by using setter methods
    • It creates the bean and defines the setter methods

    Syntax

    @Component
    public class B {
      @Autowired
      public void fun(public A a) {
        this.a = a;
      }
    }
                    

    Program

    A.java

    @Component
    public class A {
      public void display() {
        System.out.println("Hello World");
      }
    }
                        

    B.java

    @Component
    public class B{
      @Autowired
      public void fun(public A a){
        this.a = a;
      }
    }
                        

    Main.java

    public class Main {
      public static void main(String[] args){
        ApplicationContext context = new AnnotationConfigApplicationContext(B.class);
        A obj = context.getBean(A.class);
        obj.display();
      }
    }
                        

    Output

    Hello World
                        
API&M Unit-01

5) Auto Scanning

Syntax

@Component
public class A {
   ...
}
@Controller
public class B {
   @Autowired
   ...
}
@Repository
public class C {
   ...
}
@Service
public class D {
   ...
}