Lab 2.1: Java-Based Configuration
Lab 2.1: Java-Based Configuration
1: Java-based Configuration
S
Overview: In this lab, we will use Spring's Java-based
configuration to configure our beans and dependencies
– Providing Java-only metadata (no XML at all)
– We'll create a separate @Configuration class for each
tier
• We have many options here !
– We will import Java-based configurations into one root
configuration
ON
The new root lab directory where you will do all your work is:
workspace\Lab02.1
This lab contains no XML
– All configuration metadata will be written in Java - The safer
XML!
– Also, metadata will be external to our domain model
Tasks to Perform
Close all open files and projects
Create a new Java project called Lab02.1 in the workspace *
– See Lab 1.1 instructions on how to do this if you need to
– Remember to add the Spring user libraries
Don't worry about errors (Red x’s) and warnings
– They'll soon disappear
Lab 2.1: Java-based Config 3
DOMAIN Lab
MODEL
RUN Tasks to Perform
Open CatalogTest for editing
– You need to create the application context in both test methods
– It's an AnnotationConfigApplicationContext now, since you're
using @Configuration style
ARY
Java based configurations provide you with the best-of-breed
style of specifying your application requirements.
– It’s Java code so IDE support is mature and solid
– You effectively write the code you have always written to satisfy
dependencies
• But not in your application code!
– You can have logic that determines what happens in real-time
– And refactoring and cross-referencing is clean and simple
S
Overview: In this lab, we will work with the bean lifecycle
– Explore and configure different bean scopes
– Work with some of the bean lifecycle callbacks and the interfaces
Spring provides to work with them
E Tasks to Perform
Run CatalogTest again
– Go to the console view in Eclipse - do you see any output?
– How many catalogs were created?
STOP
S
Overview: In this lab, we will create profiles to customize our
bean definitions
– We'll create two profiles that have different configurations, then
test both of them
– We'll also (optionally) try out inheritance in @Configuration
classes
ON
The new root lab directory where you will do all your work is:
workspace\Lab02.3
Tasks to Perform
Close all open files and projects
–Review the rest of the test class - it has test methods for both the dev
and production profiles
Run CatalogTest as a JUnit Test
– You should see output from both profiles (1)
Modify the other two config classes to extend the base class
– Remove the common code from both types
– Add to or modify these two config classes to work correctly