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
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; } }
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. TheDemoApplication
class uses field injection (@Autowired
) to inject an instance ofMyBean
into themyBean
field.
Example 2: Constructor Injection
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(); } }
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 onMyRepository
, which is injected via constructor injection. Spring resolves these dependencies automatically because bothMyService
andMyRepository
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.
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!"; } }
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 ofGreeting
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.