HelloWorld Spring Application 1731859988
HelloWorld Spring Application 1731859988
What is Spring?
Spring is a popular, lightweight, and open-source framework for building en-
terprise Java applications. Originally introduced to simplify enterprise Java
development, Spring provides comprehensive infrastructure support for creat-
ing robust and maintainable applications.
Why Spring?
Spring addresses several common issues in traditional Java application develop-
ment:
1
2. IoC Container
The Inversion of Control (IoC) container is the core of the Spring Frame-
work. The IoC container is responsible for managing the lifecycle and configu-
ration of application objects (beans). It creates objects, manages dependencies,
and assembles beans.
Listing 1: applicationContext.xml
<?xml version=” 1 . 0 ” e n c o d i n g=”UTF−8” ?>
<beans xmlns=” h t t p : //www. s p r i n g f r a m e w o r k . o r g / schema / beans ”
x m l n s : x s i=” h t t p : //www. w3 . o r g /2001/XMLSchema−i n s t a n c e ”
x m l n s : c o n t e x t=” h t t p : //www. s p r i n g f r a m e w o r k . o r g / schema / c o n t e x t ”
x s i : s c h e m a L o c a t i o n=” h t t p : //www. s p r i n g f r a m e w o r k . o r g / schema / beans
h t t p s : //www. s p r i n g f r a m e w o r k . o r g / schema / beans / s p r i n g −b
h t t p : //www. s p r i n g f r a m e w o r k . o r g / schema / c o n t e x t
h t t p s : //www. s p r i n g f r a m e w o r k . o r g / schema / c o n t e x t / s p r i n g −
2
<bean i d=” dateTime ” c l a s s=” j a v a . time . LocalDateTime ” f a c t o r y −method=”now” />
</ beans>
@Component ( ”wmg” )
public c l a s s WishMessageGenerator {
@Autowired
public void setDateTime ( LocalDateTime dateTime ) {
t h i s . dateTime = dateTime ;
System . out . p r i n t l n ( ” S e t t e r method e x e c u t e d ” ) ;
}
public S t r i n g generateWishMessage ( S t r i n g u s e r ) {
i f ( dateTime == null ) {
throw new I l l e g a l S t a t e E x c e p t i o n ( ” LocalDateTime i s not i n i t i a l i z e d . ” ) ;
}
String greeting ;
i f ( hour < 1 2 ) {
g r e e t i n g = ”Good Morning ” ;
} e l s e i f ( hour < 1 8 ) {
g r e e t i n g = ”Good A f t e r n o o n ” ;
} else {
g r e e t i n g = ”Good Evening ” ;
}
return g r e e t i n g + ” , ” + u s e r ;
}
}
3
Step 3: Create the Main Class
In this main class, we load the Spring context, retrieve the bean, and invoke the
greeting method.
Listing 3: DependencyInjectionTest.java
package com . nt . c l i e n t ;
import o r g . s p r i n g f r a m e w o r k . c o n t e x t . s u p p o r t . F i l e S y s t e m X m l A p p l i c a t i o n C o n t e x t ;
import com . nt . s b e a n s . WishMessageGenerator ;
public c l a s s D e p e n d e n c y I n j e c t i o n T e s t {
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
F i l e S y s t e m X m l A p p l i c a t i o n C o n t e x t c t x = new F i l e S y s t e m X m l A p p l i c a t i o n C o n t e x
// R e t r i e v e t h e bean
WishMessageGenerator g e n e r a t o r = ( WishMessageGenerator ) c t x . getBean ( ”wmg
// C l o s e t h e c o n t e x t
ctx . c l o s e ( ) ;
}
}
How to Run
1. Add Spring dependencies to your project. 2. Place applicationContext.xml
in the src/com/nt/cfgs/ directory. 3. Compile and run DependencyInjectionTest.java.
Conclusion
This example demonstrates how Spring IoC and Dependency Injection simplify
Java application development by managing object creation and dependencies,
allowing for modular, testable, and maintainable code.