First Spring Application
By ski11up.com
Spring Framework provides a configuration model for Java
based enterprise applications. It has helped many organizations to focus on business logic rather than maintaining a boilerplate code. For example, Inversion of Control lifting the configuration in an XML
file to help construction and management of objects or find the required object using ComponentScan
, providing the object selection based on the name of the bean, and finally Dependency Injection using Autowired
.
Study App
Our objective will be to design an application that will provide subject-specific teachers and also if there are any other tutor services.
Let’s say we have Maths Teacher and a Tutor Service, take a look at the below diagram, this will give more information on how we can design this application.

So we need a class
for each of the subjects, we will develop MathsTeacher.java
, PhysicsTeacher.java
and ChemistryTeacher.java
and to all these teachers has common methods, for that we will develop an interface
as Teacher.java
.
For the Tutor Service let’s develop an interface
as TutorService.java
and an implementation class HappyTutorService.java
.
Project Setup
We will be using the maven
for managing project dependencies.
Download the source code from spring-demo git
repository. Delete all the files from src/main/java
, package
so that you can code along with this post.
Design Part I - Interface
First thing first, let’s develop Teacher.java
and TutorService.java
. Under the same package.
1
2
3
4
public interface Teacher {
public String getHomeWork();
public String getStudyMaterial();
}
Teacher.java
has two methods getHomeWork()
and getStudayMaterial()
. We will implement this interface
as MathsTeacher.java
, PhysicsTeacher.java
and ChemistryTeacher.java
. But lets complete the building blocks first.
1
2
3
public interface TutorService {
public String provideStudyMaterial();
}
TutorService.java
has just one method provideStudyMaterial()
.
Design Part II - Implementation
In the design shown above, we have teacher object for each of the subjects which will be used by the application via a configuration so there must be some more which needs to be added apart from just implementing the interface. Spring provides the annotation as Component
which we need to add above the class
name to make it available for the spring config to pick it up for the application.
Let’s implement the HappyTutorServce.java
.
1
2
3
4
5
6
7
8
@Component (1)
public class HappyTutorService implements TutorService {
public String provideStudyMaterial() { (2)
return "Providing study material for needy students.";
}
}
1 | Component will make sure that the spring config will find this |
2 | basic implementation of the provideStudyMaterial() |
Component Spring Annotation make sure that spring will treat this class as a bean and find it when asked for
Now implement this interface
for MathsTeacher.java
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component (1)
public class MathsTeacher implements Teacher {
@Autowired (2)
@Qualifier("happyTutorService") (3)
private TutorService tutorService;
public String getHomeWork() {
return "Maths Home Work.";
}
public String getStudyMaterial() {
return tutorService.provideStudyMaterial(); (4)
}
}
1 | Component will make sure that the spring config will find this |
2 | Dependency Injection via Autowired spring annotation |
3 | again spring annotation to find the happyTutorService bean |
4 | calling tutor service, this is magical? yes, spring will find this automatically based on Component annotation |
Autowired Spring Annotation inject the dependences on the bean and helps in providing a single object
Design Part III - Configuration
Finally, we need a way to make sure that `Component`s are available for the application.
1
2
3
4
5
@Configuration (1)
@ComponentScan("com.ski11up.springdemo") (2)
public class StudyConfig {
}
1 | Configuration will make this java class as the config for spring |
2 | ComponentScan states that the config will scan with package for bean lookup |
Configuration and ComponentScan Spring Annotation provides an alternate way of XML based spring configuration
Main Application
We have developed all the required things which we need to run Study App.
Let’s write the code for the main application.
It is in three parts:
-
Setting the application context
-
Get the bean in the context
-
Call the required methods
1
2
3
4
5
6
7
8
9
10
11
12
13
public class StudyApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(StudyConfig.class); (1)
Teacher theTeacher = context.getBean("mathsTeacher", Teacher.class); (2)
System.out.println(theTeacher.getHomeWork()); (3)
System.out.println(theTeacher.getStudyMaterial()); (4)
context.close();
}
}
1 | standard format of setting up the spring configuration |
2 | getting the bean context |
3 | ok, this is where we are calling getHomeWork() on the MathsTeacher.java |
4 | we got the dependency which we have Autowired |
Magic has happened…
Run Study App
Check the output marked by the red arrow and that’s it we have developed our first spring based application.

Source Code
You can either git fork
, git clone
or download this repository spring-demo.