Objectives
Demonstrate creation of Spring Boot Application
o Spring initializr, [Link] @SpringBootApplication,
[Link]()
Ref - [Link]
Explain the need and benefits of Spring Boot
o Makes Java development easy, avoids tedious development steps, reduces
development time, avoids writing boilerplate code, provides embedded tomcat
server, avoid XML configuration
Ref - [Link]
Demonstrate loading bean from spring configuration file
o Spring configuration xml, spring xml schema [Link], <bean>, id, class,
<constructor-arg>, <property>, name, value, ClassPathXmlApplicationContext,
ApplicationContext, [Link](), singleton scope, prototype scope
Ref - [Link]
reference/[Link]
IoC Container - [Link]
framework-reference/[Link]#beans
Scopes - [Link]
framework-reference/[Link]#beans-factory-scopes
Constructor Injection -
[Link]
reference/[Link]#beans-constructor-injection
Setter method injection -
[Link]
reference/[Link]#beans-setter-injection
Demonstrate inclusion of logging in Spring Boot Application
o [Link], [Link], [Link], [Link], LoggerFactory,
Logger, log levels (trace, debug, info, warn, error)
Ref - [Link]
[Link]
Hands on 1
Create a Spring Web Project using Maven
Follow steps below to create a project:
1. Go to [Link]
2. Change Group as “[Link]”
3. Change Artifact Id as “spring-learn”
4. Select Spring Boot DevTools and Spring Web
5. Create and download the project as zip
6. Extract the zip in root folder to Eclipse Workspace
7. Build the project using ‘mvn clean package -
[Link]=[Link] -[Link]=6050 -
[Link]=[Link] -[Link]=6050 -
[Link]=123456’ command in command line
8. Import the project in Eclipse "File > Import > Maven > Existing Maven
Projects > Click Browse and select extracted folder > Finish"
9. Include logs to verify if main() method of SpringLearnApplication.
10. Run the SpringLearnApplication class.
SME to walk through the following aspects related to the project created:
1. src/main/java - Folder with application code
2. src/main/resources - Folder for application configuration
3. src/test/java - Folder with code for testing the application
4. [Link] - Walkthrough the main() method.
5. Purpose of @SpringBootApplication annotation
6. [Link]
1. Walkthrough all the configuration defined in XML file.
2. Open 'Dependency Hierarchy' and show the dependency tree.
Hands on 2
Spring Core – Load SimpleDateFormat from Spring
Configuration XML
SimpleDateFormat with the pattern ‘dd/MM/yyyy’ is created in multiple places
of an application. To avoid creation of SimpleDateFormat in multiple places,
define a bean in Spring XML Configuration file and retrieve the date.
Follow steps below to implement:
Create spring configuration file date-
[Link] in src/main/resources folder of 'spring-learn' project
Open [Link]
framework-reference/[Link]#beans-factory-metadata
Copy the XML defined in the section of previous step URL and paste it
into [Link]
Define bean tag in the XML with for date format. Refer code below.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<bean id="dateFormat" class="[Link]">
<constructor-arg value="dd/MM/yyyy" />
</bean>
</beans>
Create new method displayDate() in [Link]
In displayDate() method create the ApplicationContext. Refer code
below:
ApplicationContext context = new ClassPathXmlApplicationContext("date-
[Link]");
Get the dateFormat using getBean() method. Refer code below.
SimpleDateFormat format = [Link]("dateFormat",
[Link]);
Using the format variable try to parse string '31/12/2018' to Date class
and display the result using [Link].
Run the application as 'Java Application' and check the result in console
log output.
Troubleshooting Tips
If the tomcat port has a conflict and the server is not starting include the below
property in [Link] file in src/main/resources folder.
Hands on 3
Spring Core - Incorporate Logging
Incorporate logging in the Spring Boot project created in previous hands on.
Refer steps below:
Create [Link] if not yet created
in src/main/resources folder
Add below lines in [Link]
[Link]=info
[Link]=debug
[Link]=%d{yyMMdd}|%d{HH:mm:[Link]}|%-20.20thread|%5p|%-
25.25logger{25}|%25M|%m%n
In [Link] include the following imports:
import [Link];
import [Link];
Include the below static variable in [Link]:
private static final Logger LOGGER =
[Link]([Link]);
Include info log on start and end of method. Debug log for displaying the
date (refer code below)
public void displayDate() {
[Link](“START”);
//..
[Link](date);
//..
[Link](“END”);
}
IMPORTANT NOTE: Going forward all methods should incorporate logging as
specified above. Never use [Link]().
Hands on 4
Spring Core – Load Country from Spring
Configuration XML
An airlines website is going to support booking on four countries. There will be
a drop down on the home page of this website to select the respective
country. It is also important to store the two-character ISO code of each
country.
Code Name
US United States
DE Germany
IN India
JP Japan
Above data has to be stored in spring configuration file. Write a program to
read this configuration file and display the details.
Steps to implement
Pick any one of your choice country to configure in Spring XML
configuration named [Link].
Create a bean tag in spring configuration for country and set the
property and values
<bean id="country" class="[Link]">
<property name="code" value="IN" />
<property name="name" value="India" />
</bean>
Create Country class with following aspects:
o Instance variables for code and name
o Implement empty parameter constructor with inclusion of debug
log within the constructor with log message as “Inside Country
Constructor.”
o Generate getters and setters with inclusion of debug with relevant
message within each setter and getter method.
o Generate toString() method
Create a method displayCountry() in [Link], which
will read the country bean from spring configuration file and display the
country
details. ClassPathXmlApplicationContext, ApplicationContext and conte
[Link](“beanId”, [Link]). Refer sample code for
displayCountry() method below.
ApplicationContext context = new
ClassPathXmlApplicationContext("[Link]");
Country country = (Country) [Link]("country", [Link]);
[Link]("Country : {}", [Link]());
Invoke displayCountry() method in main() method
of [Link].
Execute main() method and check the logs to find out which
constructors and methods were invoked.
SME to provide more detailing about the following aspects:
bean tag, id attribute, class attribute, property tag, name attribute, value
attribute
ApplicationContext, ClassPathXmlApplicationContext
What exactly happens when [Link]() is invoked
Hands on 5
Spring Core – Demonstration of Singleton Scope and
Prototype Scope
The Country bean done in the previous hands on will be used to demonstrate
the scopes in Spring. Implement the steps below.
Follow steps below to demonstrate Singleton Scope
Include a line in displayCountry() to get country bean reference one
more time from the same application context. Only the third line of the
below code snippet should be copied and pasted.
ApplicationContext context = new
ClassPathXmlApplicationContext("[Link]");
Country country = [Link]("country", [Link]);
Country anotherCountry = [Link]("country", [Link]);
The constructor will be called only once, which means that only one
instance of Country bean is created
Follow steps below to demonstrate Prototype Scope
Include scope="prototype" attribute in bean definition xml.
<bean id="country" class="[Link]"
scope="prototype">
Run the application
Constructor will be called twice, which means that two instances of
country is created.
Hands on 6
Spring Core – Load list of countries from Spring
Configuration XML
Our main objective was to retrieve the list of four countries for the airlines
website. Refer steps below to get this incorporated.
Create a separate bean for each of the four country in [Link].
Create an ArrayList of Country in [Link]. Refer code below.
<bean id="countryList" class="[Link]">
<constructor-arg>
<list>
<ref bean="in"></ref>
<ref bean="us"></ref>
<ref bean="de"></ref>
<ref bean="jp"></ref>
</list>
</constructor-arg>
</bean>
Include new method displayCountries() in [Link]
In displayCountries() read the country list created above
Display the list of countries as debug log.
SME to provide detailing on below aspects:
<list>
<ref>
bean attribute
IMPORTANT NOTE: Do not forget to include the start and end logs in this
new method.