Bean Creation -Setters , Constructors

 Certainly! Here are a few examples of creating objects using the Spring Framework's container, particularly with annotations (@Component, @Autowired) for dependency injection:

Example 1: Basic Bean Creation

  1. Define a Bean Class:


    package com.example.demo; import org.springframework.stereotype.Component; @Component // Marks this class as a Spring bean public class MyBean { private String message = "Hello, World!"; public String getMessage() { return message; } }
  2. Inject and Use the Bean:


    package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { @Autowired private MyBean myBean; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } // Example usage of the bean // Assuming this method is in a Spring-managed component public void useMyBean() { System.out.println(myBean.getMessage()); } }

    In this example, MyBean is automatically detected by Spring due to the @Component annotation. The DemoApplication class uses field injection (@Autowired) to inject an instance of MyBean into the myBean field.

Example 2: Constructor Injection

  1. Define a Service Class and a Repository:


    package com.example.demo; import org.springframework.stereotype.Repository; @Repository // Marks this class as a repository (a type of component) public class MyRepository { public String getData() { return "Data from MyRepository"; } }

    package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service // Marks this class as a service (a type of component) public class MyService { private final MyRepository myRepository; @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } public String serveData() { return myRepository.getData(); } }
  2. Use the Service:


    package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication implements CommandLineRunner { private final MyService myService; @Autowired public DemoApplication(MyService myService) { this.myService = myService; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println(myService.serveData()); } }

    In this example, MyService depends on MyRepository, which is injected via constructor injection. Spring resolves these dependencies automatically because both MyService and MyRepository are annotated (@Service and @Repository, respectively).

Example 3: Qualifier for Dependency Resolution

Sometimes, when you have multiple beans of the same type, you might need to specify which one to inject.

  1. Define Multiple Implementations:


    package com.example.demo; import org.springframework.stereotype.Component; @Component("englishGreeting") // Qualify this bean with a specific name public class EnglishGreeting implements Greeting { @Override public String greet() { return "Hello!"; } }

    package com.example.demo; import org.springframework.stereotype.Component; @Component("spanishGreeting") // Qualify this bean with a specific name public class SpanishGreeting implements Greeting { @Override public String greet() { return "¡Hola!"; } }
  2. Inject with Qualifier:


    package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class GreetingService { private final Greeting englishGreeting; private final Greeting spanishGreeting; @Autowired public GreetingService(@Qualifier("englishGreeting") Greeting englishGreeting, @Qualifier("spanishGreeting") Greeting spanishGreeting) { this.englishGreeting = englishGreeting; this.spanishGreeting = spanishGreeting; } public void greetInDifferentLanguages() { System.out.println("In English: " + englishGreeting.greet()); System.out.println("In Spanish: " + spanishGreeting.greet()); } }

    In this example, GreetingService uses @Qualifier with constructor injection to specify which implementation of Greeting to inject. Spring resolves these dependencies based on the names provided to @Qualifier.

These examples showcase different ways to create and wire beans using the Spring Framework, demonstrating various dependency injection techniques and annotations available in Spring.

Daily Knowledge Journey: A Quest for Learning

Object Class

 The Object class in Java is the root of the class hierarchy and serves as the superclass for all other classes. It provides fundamental me...