Q.1. Write A Spring Program Using Applicationcontext. Pom - XML
Q.1. Write A Spring Program Using Applicationcontext. Pom - XML
Pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
</dependencies>
//AppConfig.java
import
org.springframework.context.annotation.AnnotationConfigApplicatio
nContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example.spring")
public class AppConfig {
}
// Main.java
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicatio
nContext;
Output :-
Good Morning!
Hello from MessageService!
Q.3. Write a spring program for bean scope using prototype.
pom.xml
<dependencies> >> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version> <!-- Or the latest version -->
</dependency> >> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.24</version> <!-- Or the latest version -->
</dependency> >> </dependencies>
MessageService.java (Bean with prototype scope)
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
importorg.springframework.beans.factory.config.ConfigurableBeanFa
ctory;
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // Prototype
scope
public class MessageService { >>> private String message;
// Constructor >> public MessageService() {
this.message = "Hello from MessageService (Prototype Scope)!";
} >> public String getMessage() { > return message;
} >> public void setMessage(String message) {
this.message = message; >> } >> }
AppConfig.java (Java-based configuration)
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example") // Specifies the
base package to scan for @Component annotated classes
public class AppConfig {
// Beans will be auto-discovered by @ComponentScan
}
App.java
Importorg.springframework.context.annotation.AnnotationConfigAp
plicationContext;
import com.example.MessageService;
public class App { >> public static void main(String[] args) {
// Initialize Spring context using Java configuration
try (AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class)) {
// Retrieve beans from Spring container
MessageService messageService1 =
context.getBean(MessageService.class);
MessageService messageService2 =
context.getBean(MessageService.class);
// Check if both beans are different instances (demonstrating
prototype scope)
System.out.println("messageService1 hashCode: " +
messageService1.hashCode());
System.out.println("messageService2 hashCode: " +
messageService2.hashCode());
// Print the messages
System.out.println(messageService1.getMessage());
System.out.println(messageService2.getMessage());
// Ensure that the two instances are different
if (messageService1 != messageService2) {
System.out.println("messageService1 and messageService2
are different instances (Prototype Scope)"); >> } else {
System.out.println("messageService1 and messageService2
are the same instance");
} >> } >> } >> }
Output :- messageService1 hashCode: 12345678
messageService2 hashCode: 87654321
Hello from MessageService (Prototype Scope)!
Hello from MessageService (Prototype Scope)!
messageService1 and messageService2 are different
instances (Prototype Scope)