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

HelloWorld Spring Application 1731859988

Uploaded by

arunpasham
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)
11 views4 pages

HelloWorld Spring Application 1731859988

Uploaded by

arunpasham
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/ 4

Introduction to Spring Framework

Java Learning Series - Day 1

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:

• Complexity of Enterprise Java Beans (EJBs): Traditional EJBs are


complex and heavy, requiring extensive setup and configuration.
• Tight Coupling: Without a framework, Java components often have
tight dependencies, making it harder to modify and test individual com-
ponents.

• Inflexibility in Testing: Spring promotes dependency injection, making


it easier to substitute dependencies for testing.
• Boilerplate Code Reduction: With Spring, repetitive tasks (e.g., trans-
action management) are handled declaratively, reducing boilerplate code.

Core Concepts of Spring


1. Beans
In Spring, a bean is an object that is instantiated, managed, and configured
by the Spring IoC container. Beans are defined in XML configuration files,
annotated with Spring annotations, or defined programmatically.

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.

• ApplicationContext: The most commonly used container in Spring,


it provides additional features like event propagation, declarative mecha-
nisms, and many integrations.

3. Dependency Injection (DI)


Dependency Injection is a design pattern where the dependencies of an object
are provided externally rather than hard-coded within the object. This enables
loose coupling and more maintainable code. In Spring, DI can be achieved
through:
• Constructor Injection: Dependencies are injected via the constructor.

• Setter Injection: Dependencies are injected via setter methods.

Example Code: Setting Up a Simple Spring Ap-


plication
Here’s a basic example using Spring to manage a WishMessageGenerator bean.

Step 1: Define the XML Configuration File (applicationContext.xml)


In this XML file, we enable component scanning and define a LocalDateTime
bean:

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 −

< !−− Enable component s c a n n i n g f o r S p r i n g t o d e t e c t and r e g i s t e r b ea n s w i t h


<c on t e xt : co m p on e nt −s c a n base−package=”com . nt . s b e a n s ” />

< !−− D e f i n e LocalDateTime bean u s i n g f a c t o r y method −−>

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>

Step 2: Create the WishMessageGenerator Class


This class has a method to generate a greeting message based on the time of
day. The class uses LocalDateTime to retrieve the current hour.
Listing 2: WishMessageGenerator.java
package com . nt . s b e a n s ;

import j a v a . time . LocalDateTime ;


import o r g . s p r i n g f r a m e w o r k . beans . f a c t o r y . a n n o t a t i o n . Autowired ;
import o r g . s p r i n g f r a m e w o r k . s t e r e o t y p e . Component ;

@Component ( ”wmg” )
public c l a s s WishMessageGenerator {

private LocalDateTime dateTime ;

@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 . ” ) ;
}

int hour = dateTime . getHour ( ) ;


System . out . p r i n t l n ( ” B u s i n e s s method e x e c u t 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

// Generate and p r i n t t h e w i s h message


S t r i n g r e s u l t = g e n e r a t o r . generateWishMessage ( ” K r i s ” ) ;
System . out . p r i n t l n ( ” R e s u l t : ” + r e s u l t ) ;

// 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.

You might also like