0% found this document useful (0 votes)
9 views10 pages

Ass3 Debi

Uploaded by

devildada0987
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)
9 views10 pages

Ass3 Debi

Uploaded by

devildada0987
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/ 10

Assignment - III

Topic: Creating a Maven Project, Defining a POJO Class, Generating


runtime object using Spring IoC and Injecting Dependencies
Name: Debi Prasad Rath
Registration Number: 2251018009
Section: I

1. Write a POJO class named Person with the following details


a. Name (String)
b. Age (int)
c. City (String)
d. Phone number (List)
take the input from user and print the details.

CODE:
package Assignment3;
import java.util.*;
class Person{
String name, city;
int age;
List<String> phoneNumber=new ArrayList<>();

Person(String name, String city, int age,List<String> phoneNumber ){


this.name=name;
this.city=city;
this.age=age;
this.phoneNumber=phoneNumber;
}

public void display(){


System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("City: "+city);
System.out.println("Phone Number: "+phoneNumber);
}
}
public class Q1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);

System.out.println("Enter your name: ");


String name=sc.nextLine();
System.out.println("Enter the city: ");
String city=sc.nextLine();
System.out.println("Enter your age: ");
int age=sc.nextInt();
sc.nextLine();
List<String> phoneNumber=new ArrayList<>();
System.out.println("Enter the Phone number: ");
phoneNumber.add(sc.nextLine());

Person p=new Person(name, city, age, phoneNumber);


p.display();
}
}
OUTPUT:

2. Write a Java Program to create a class Student with the following instance
members: -
a. Student ID (int)
b. Student Name (String)
c. Student Branch (String)
d. Student Phone Numbers (List)
e. Student Course name with fees (Map)
Use setter and getter methods to initialize and fetch the details. Using Spring Core -
IoC, inject the Object using XML Configuration
CODE:
Student.java
package com.A3Q2;
import java.util.*;
public class Student {
int studentId;
String studentName, studentBranch;
List<String> phoneNumbers;
Map<String,Integer> courseFees;

public int getStudentId() {


return studentId;
}

public void setStudentId(int studentId) {


this.studentId = studentId;
}

public String getStudentName() {


return studentName;
}

public void setStudentName(String studentName) {


this.studentName = studentName;
}

public String getStudentBranch() {


return studentBranch;
}

public void setStudentBranch(String studentBranch) {


this.studentBranch = studentBranch;
}

public List<String> getPhoneNumbers() {


return phoneNumbers;
}
public void setPhoneNumbers(List<String> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}

public Map<String, Integer> getCourseFees() {


return courseFees;
}

public void setCourseFees(Map<String, Integer> courseFees) {


this.courseFees = courseFees;
}

public void display(){


System.out.println("Student ID: "+studentId);
System.out.println("Name: "+studentName);
System.out.println("Branch: "+studentBranch);
System.out.println("Phone Numbers: "+phoneNumbers);
System.out.println("Course Fees: "+courseFees);
}
}

Bean.xml
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="Message" class="com.A2Q5.Message">


<property name="message" value="Hello XYZ !"/>
</bean>
</beans>

App.java
package com.A3Q2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Student s1 = (Student) context.getBean("Student");
s1.display();
}
}
OUTPUT:

3. Write a Java Program to create a class Employee and Address with the following
instance members: -
a. class Employee
i. Employee ID (int)
ii. Employee Name (String)
iii. Employee Department (String)
iv. Employee Address (Reference type)
b. Class Address
i. Street (String)
ii. City (String)
iii. State (String)
iv. Pin code (int)
Using Spring IoC, Constructor approach to inject the Object.

CODE:
Employee.java
package com.A3Q3;

public class Employee {


int employeeId;
String employeeName;
String employeeDept;
Address address;

Employee(int employeeId, String employeeName, String employeeDept, Address address){


this.employeeId=employeeId;
this.employeeName=employeeName;
this.employeeDept=employeeDept;
this.address=address;
}

public void display(){


System.out.println("ID: "+employeeId);
System.out.println("Name: "+employeeName);
System.out.println("Department: "+employeeDept);
System.out.println("Address: "+address);
}
}

Address.java
package com.A3Q3;

public class Address {


String city, state, country;
int pincode;

public Address(String city, String state, String country, int pincode) {


this.city = city;
this.state=state;
this.country=country;
this.pincode=pincode;
}

@Override
public String toString() {
return "{" +
"city='" + city + '\'' +
", state='" + state + '\'' +
", country='" + country + '\'' +
", pincode=" + pincode +
'}';
}
}

Bean.xml
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="Address" class="com.A3Q3.Address">


<constructor-arg index="0" value="Bhubaneswar"/>
<constructor-arg index="1" value="Odisha"/>
<constructor-arg index="2" value="India"/>
<constructor-arg index="3" value="751030"/>
</bean>

<bean id="Employee" class="com.A3Q3.Employee">


<constructor-arg index="0" value="2141013059"/>
<constructor-arg index="1" value="Chandan Kumar Mallick"/>
<constructor-arg index="2" value="Computer Science"/>
<constructor-arg index="3" ref="Address"/>
</bean>

</beans>
App.java
package com.A3Q3;

import com.A3Q3.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {


public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Employee e1 = (Employee) context.getBean("Employee");
e1.display();
}
}
OUTPUT:

4. Write a Java Program to create a class Customer with the following instance
members:
a. Customer Name (String)
b. Customer Phone Number (List)
c. Customer Email ID (String)
Using Spring IoC, Annotation approach to inject the Object

CODE:
Customer.java
package com.A3Q4;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;

@Component
public class Customer {
String name;
List<String> phoneNumber;
String emailId;

public Customer(String name, List<String> phoneNumber, String emailId){


this.name=name;
this.phoneNumber=phoneNumber;
this.emailId=emailId;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public List<String> getPhoneNumber() {


return phoneNumber;
}

public void setPhoneNumber(List<String> phoneNumber) {


this.phoneNumber = phoneNumber;
}

public String getEmailId() {


return emailId;
}

public void setEmailId(String emailId) {


this.emailId = emailId;
}

@Override
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", phoneNumber=" + phoneNumber +
", emailId='" + emailId + '\'' +
'}';
}

AppConfiguration.java:
package com.A3Q4;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.*;

@Configuration
@ComponentScan(basePackages="com.A3Q4")
public class AppConfiguration {
@Bean
public String name(){
return "Chandan Kumar Mallick";
}

@Bean
public List<String> phoneNumber(){
return Arrays.asList("7327888730","9692953033");
}

@Bean
public String emailId(){
return "[email protected]";
}
@Bean
public Customer customer(){
return new Customer(name(), phoneNumber(), emailId());
}
}

App.java
package com.A3Q4;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {


public static void main(String[] args) {
ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfiguration.class);
Customer c= context.getBean(Customer.class);
System.out.println(c);
}
}

OUTPUT:

5. Write a simple Spring Application, to understand the life of beans.

CODE:
BeanLifeCycle.java

package com.A3Q5;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.stereotype.Component;

@Component
public class BeanLifeCycle {
public BeanLifeCycle(){
System.out.println("Bean is being created: constructor.");
}

@PostConstruct
public void init(){
System.out.println("Bean is initialized: @PostConstruct");
}

public void message(){


System.out.println("Bean method called: message()");
}

@PreDestroy
public void destroy(){
System.out.println("Bean is being destoryed: @PreDestroy");
}
}
AppConfig.java

package com.A3Q5;

import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
public BeanLifeCycle bean(){
return new BeanLifeCycle();
}
}
App.java

package com.A3Q5;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext context= SpringApplication.run(App.class, args);
BeanLifeCycle bean=context.getBean(BeanLifeCycle.class);
bean.message();
context.close();
}
}
OUTPUT:

You might also like