0% found this document useful (0 votes)
1 views9 pages

Q.1. Write A Spring Program Using Applicationcontext. Pom - XML

Uploaded by

pmovies347
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views9 pages

Q.1. Write A Spring Program Using Applicationcontext. Pom - XML

Uploaded by

pmovies347
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Q.1. Write a spring program using ApplicationContext.

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;

public class MainApplication {


public static void main(String[] args) {
// Create an instance of ApplicationContext
ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);

// Get the UserService bean from the context


UserService userService = context.getBean(UserService.class);

// Call the printMessage method


userService.printMessage();

// Close the context


((AnnotationConfigApplicationContext) context).close();
}
}
Q.2. Write a spring program using BeanFactory
Pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.24</version> <!-- Use the latest version -->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version> <!-- Use the latest version -->
</dependency>
</dependencies>
MessageService.java
public class MessageService {
public String getMessage() {
return "Hello from MessageService!"; >> } >> }
GreetingService.java
public class GreetingService {
public String getGreeting() {
return "Good Morning!"; >> } >> }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/bea
ns
http://www.springframework.org/schema/beans/spring-
beans-5.3.xsd">
<!-- Define the MessageService Bean -->
<bean id="messageService" class="MessageService"/>
<!-- Define the GreetingService Bean -->
<bean id="greetingService" class="GreetingService"/>
</beans>
Main.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class App {
public static void main(String[] args) {
// Load the bean configuration file
Resource resource = new ClassPathResource("beans.xml");
// Initialize BeanFactory (Deprecated in newer versions of Spring)
// To use BeanFactory, we use the XmlBeanFactory which is
deprecated in Spring 3.1
// As of Spring 3.1, it's recommended to use
AnnotationConfigApplicationContext
BeanFactory factory = new XmlBeanFactory(resource);

// Get beans from the BeanFactory


MessageService messageService = (MessageService)
factory.getBean("messageService");
GreetingService greetingService = (GreetingService)
factory.getBean("greetingService");

// Use the beans


System.out.println(greetingService.getGreeting());
System.out.println(messageService.getMessage());
}
}

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)

You might also like