0% found this document useful (0 votes)
22 views4 pages

Constructor Injection in Java Applications

Uploaded by

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

Constructor Injection in Java Applications

Uploaded by

rahman khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Task 1 — Hospital with Doctor and Equipment Injection

🎯 Objective:
Demonstrate constructor-based dependency injection using AppConfig where a Hospital
depends on both a Doctor and an Equipment object.

🧱 Class-wise Design

1️
.Doctor Class
Attributes:

String name

String specialization

Constructor:

Initialize both fields

Method:

showDoctor() → prints doctor details

2️
.Equipment Class
Attributes:

String equipmentName

double cost

Constructor:

Initialize both fields

Method:

showEquipment() → prints equipment info

3️
.Hospital Class
Attributes:

String hospitalName

Doctor doctor

Equipment equipment

Constructor:

Inject all three via constructor

Method:

showDetails() → display full hospital info

4️
.AppConfig Class
Responsibilities:

Annotated with @Configuration


Define beans for Doctor, Equipment, and Hospital

Inject supporting beans into Hospital constructor

5️
. MainApp Class
Responsibilities:

Load AnnotationConfigApplicationContext

Retrieve Hospital bean

Call showDetails()

📘 Expected Output:

Hospital: City Care Hospital


Doctor: Dr. Meena Gupta (Cardiologist)
Equipment: ECG Machine (Cost: ₹2,50,000)
===================================================================================
====================
🧩 Task 2 — University with Professor and Course Injection

🎯 Objective:
Show constructor-based injection in a university system where a University has a
Professor and a Course.

🧱 Class-wise Design

1️
.Professor Class
Attributes:

String name

int yearsOfExperience

Constructor:

Initialize both fields

Method:

showProfessor() → prints professor details

2️
.Course Class
Attributes:

String courseName

int duration (in months)

Constructor:

Initialize both fields

Method:

showCourse() → prints course details


3️
.University Class
Attributes:

String universityName

Professor professor

Course course

Constructor:

Inject all three via constructor

Method:

showDetails() → print complete info

4️
.AppConfig Class

@Configuration + @Bean methods for all three

Inject Professor and Course into University constructor

5️
.MainApp Class

Load AnnotationConfigApplicationContext

Get University bean

Call showDetails()

📘 Expected Output:

University: National Institute of Technology


Professor: Dr. Suresh Iyer (15 years experience)
Course: Data Science (Duration: 6 months)
===================================================================================
=====
🧩 Task 3 — MusicBand with Instruments Injection (List Collection Constructor
Injection)

🎯 Objective:
Demonstrate constructor-based list injection where a band performs with multiple
instruments.

🧱 Class-wise Design

1️
. Instrument Class
Attributes:

String name

String type

Constructor:

Initialize both fields

Method:
showInstrument() → display instrument info

2️
.MusicBand Class
Attributes:

String bandName

List<Instrument> instruments

Constructor:

Inject both bandName and instruments via constructor

Method:

showBandDetails() → print band name and all instruments

3️
.AppConfig Class
Responsibilities:

@Configuration

Define multiple Instrument beans (e.g., Guitar, Drums, Keyboard)

Define MusicBand bean and inject list via constructor

4. MainApp Class
Responsibilities:

Load AnnotationConfigApplicationContext

Get MusicBand bean

Call showBandDetails()

📘 Expected Output:

Music Band: RockNation


Instruments Used:
Name: Guitar (Type: String)
Name: Drums (Type: Percussion)
Name: Keyboard (Type: Electronic)

You might also like