Spring 3.0 tutorial – part 3 (Setter DI with annotations)

This is a very simple Spring setter DI (dependency injection tutorial) using annotations. This assumes that you have gone through the “Setting up Java, Maven and Eclipse” tutorial. To recap:

1) Java, Maven, and eclipse are set up.
2) You have run the Maven archetype:generate command to create a skeleton structure.

3) import the project in eclipse.
4) You need to have a project structure as shown below with the relevant artifacts once gone through all the steps outlined below.


Step 1: pom.xml file with spring dependendencies. You can try using different (i.e. latest version of spring).

Spring Architecture

Spring Architecture

Step 2: Define the DAO layer Java interface and implementation for Course.

Spring annotations for DI

Spring annotations for DI

Step 3: Define the Service layer Java interface and implementation for Course.

Note: Note that “CoursDaoImpl” need to be injected via Spring into the “CourseServiceImpl”.

Step 4: The Spring context XML file that wires up dependnecies. It defines the DAO and Service Java classes with “CoursDaoImpl” being injected into “CourseServiceImpl” via the setter injection using annotations.


Step 5: Finally, bootsrap the “courseApplicationContext.xml” via the App.java with the main method to execute this stand alone.


Java Interview FAQs

800+ Java Interview Q&As