Adp Ass 4
Adp Ass 4
1. Write a Java Program to show Loosely Coupling using Spring XML configuration and
Constructor-based Dependency Injection using the following:
a. PaymentMethod Interface
b. CreditCard Class implements the PaymentMethod Interface
c. PayPal Class implements the PaymentMethod Interface
d. ShoppingCart Class uses the PaymentMethod instance to process payments.
e. App Class to run the application
Answer :-
1. PaymentMethod Interface
2. CreditCard Class
3. PayPal Class
4. ShoppingCart Class
public class ShoppingCart {
private PaymentMethod paymentMethod;
public ShoppingCart(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
} public void checkout(double amount) {
paymentMethod.processPayment(amount);
}}
5. App Class (to run the application)
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Perform checkout
cart.checkout(250.0);
}
}
</beans>
Output :-
Payment of $250.0 processed through Credit Card.
<constructor-arg ref="payPal"/>
Payment of $250.0 processed through PayPal.
2. Write a Java Program showing setter-based dependency injection with reference in XML
Configuration using the following.
a. EmailService class with a method sendEmail(String msg)()
b. SMSService class with a method sendSMS(String msg)()
c. NotificationService depends on both the above classes with setter methods
Answer :-
1. Define the EmailService Class
Answer –
1. Create the UserRepository Class
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository {
@Service
public class UserService {
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "your.package.name")
public class AppConfig {
}
4. Define the App Class
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Output :-
User has been saved to the repository.
Answer :-
Step 1: Define the GreetingService Interface
import org.springframework.stereotype.Service;
@Service
public class GreetingServiceImplement implements GreetingService {
@Override
public void sayGreeting() {
System.out.println("Hello, Welcome to Spring using component-scan and annotations!");
}
}
Output :-
Hello, Welcome to Spring using component-scan and annotations!
5. Write POJO Java program to convert tightly coupled code into loosely coupled code
[1] Create a parent class A with a method display(). Create another class B that inherits
class A and contains a method display(). Create a main class to call the display
method.
[2] Create a class LightBulb with a method SwitchOn(). Create another class Switch
that has an object of the LightBulb class and another method Operate(). Inside the
Operate() method call the SwitchOn() method
Answer –
Part 1: Convert Tightly Coupled Code into Loosely Coupled Code
Tightly Coupled Code (Inheritance)
class A {
public void display() {
System.out.println("Display method in class A");
}
}
class B extends A {
@Override
public void display() {
System.out.println("Display method in class B");
}
}
public class MainTightlyCoupled {
public static void main(String[] args) {
B obj = new B(); // Tightly coupled
obj.display(); // This will call the display method in class B
}
}
Output :-
Display method in class B
Loosely Coupled Code (Using Interfaces)
interface Displayable {
void display();
}
class A implements Displayable {
@Override
public void display() {
System.out.println("Display method in class A");
}
}
class B implements Displayable {
@Override
public void display() {
System.out.println("Display method in class B");
}
}
public class MainLooselyCoupled {
public static void main(String[] args) {
Displayable obj = new B();
obj.display();
}
}
Output :-
Display method in class B
Output :-
LightBulb is switched on
6. Write a simple SpringBoot Project and add the Spring Web and Spring Boot Dev Tools
dependencies. Execute the application.
Answer –
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Write the Controller Class
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@GetMapping("/greet")
public String greet() {
return "Hello, Welcome to Spring Boot!";
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemoApplication {
Output :-
Hello, Welcome to Spring Boot!
Answer –
Create the Employee Class
package com.example.demo;
public class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Employee [Name: " + name + ", Age: " + age + "]";
}
public String getName() {
return name;
}
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
@GetMapping("/employee")
public String getEmployee() {
Employee emp = new Employee("John Doe", 30);
return emp.toString();
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemoApplication {
Output :-
Employee [Name: John Doe, Age: 30]
8. How does Spring Boot use @ConditionalOnClass to trigger auto-configuration?
Answer –
In Spring Boot, auto-configuration is a powerful feature that configures application beans and settings
based on classpath content and other environment settings. This is usually done to reduce the need for
manual configuration of beans and services.
One key annotation used for this purpose is @ConditionalOnClass, which allows certain auto-
configuration classes to be loaded only if specific classes are present on the classpath.
Answer –
In Spring Boot, auto-configuration can be controlled based on various conditions, including the
presence of certain properties. The annotation @ConditionalOnProperty is used to enable or disable
auto-configuration or beans based on the presence and value of specific properties in the application's
configuration files (like application.properties or application.yml).
Answer –
In Spring Boot, auto-configuration allows for the automatic configuration of beans based on certain
conditions, such as the presence of classes, properties, or other beans in the application context. One of
the key annotations used in this process is @ConditionalOnBean, which controls auto-configuration or
bean creation based on the presence of specific beans in the Spring context.
11. What is the purpose of the spring-boot-starter-parent in a Spring Boot application, and how
do we dive deeper into the structure of any dependency used within the project?
Answer –
The spring-boot-starter-parent is a parent POM (Project Object Model) in Maven that provides default
configurations and dependency management for Spring Boot projects. It simplifies the setup and
configuration of Spring Boot applications by offering default versions of libraries, plugins, and settings,
enabling developers to focus on application development rather than boilerplate configuration.