Programming to Interface concept in Spring
Overview
In previous chapter we implemented Traditional JDBC in Spring. We also saw the disadvantages of this approach. We will now implement Spring-JDBC for storing and retrieving employee information from the Database. But before we do that, lets understand the concept of Programming to Interface concept. In this chapter we will implement the Programming to interface using Spring Now suppose if we have to replace the EmployeeDAO.java class which uses Traditional JDBC with another class named EmployeeDAOUsingSpringJDBC.java which uses Spring JDBC.This will also involve changing our EmployeeService.java class to replace the instance of EmployeeDAO.java with EmployeeDAOUsingSpringJDBC.java. So there is still some tight coupling between the EmployeeService and EmployeeDAO.java class. In future if we implement EmployeeDAOUsingHibernate.java then again EmployeeService.java will have to be changed. Now this is not good coding practice and may lead to problems especially if the code has many Service classes. This issue is resolved by using Programming to Interface in Spring. Lets see how this is implemented. The design will be as follows.
Lets Begin
We will create Eclipse Maven project as follows-
We will implement an additional layer between the Service and the DAO layer. This additional layer will be an interface. The Service class will always reference this interface to interact with the DAO layer which implements this interface. If in future we have a new DAO implementation there will be no change in the Service layer or the DAO interface. We will only have to make some small changes in the configuration file application.xml. So lets begin this implementation. First rename the EmployeeDAO.java having the traditional JDBC code to EmployeeDAOImplUsingJDBC.java. Next create a interface named as EmployeeDAO.java having the add and getAllEmployees methods as follows.
package com.javainuse.dao;
import java.util.List;
import com.javainuse.domain.Employee;
public interface EmployeeDAO {
public void addNewEmployee(Employee employee);
public List<Employee> getAllEmployees();
}
Modify the EmployeeDAOUsingJDBC.java to implement the new EmployeeDAO.java interface.
It already has the interface methods implemented.
package com.javainuse.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.javainuse.domain.Employee;
public class EmployeeDAOImplUsingJDBC implements EmployeeDAO {
//Create Employee Table in constructor.
public EmployeeDAOImplUsingJDBC() {
try {
Class.forName("org.hsqldb.jdbcDriver");
createEmployeeTable();
}
catch (ClassNotFoundException e) {
System.out.println("Exception occured in DAO constructor");
}
}
private static void createEmployeeTable() {
try {
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:hsqldb:file:database.dat;shutdown=true", "sa", "");
Statement stmt = con.createStatement();
stmt.executeUpdate(
"create table Employee(empId VARCHAR(20), name VARCHAR(50), designation VARCHAR(50),salary VARCHAR(50))");
System.out.println("Created new table Employee");
}
finally {
if (con != null)
con.close();
}
}
catch (SQLException e) {
System.out.println("Employee table has already been created...");
}
}
@Override
public void addNewEmployee(Employee employee) {
try {
Connection con = null;
PreparedStatement insertEmployee = null;
try {
con = DriverManager.getConnection("jdbc:hsqldb:file:database.dat;shutdown=true", "sa", "");
insertEmployee =
con.prepareStatement("insert into Employee (empid, name, designation) values (?, ?, ?)");
insertEmployee.setString(1, employee.getEmpId());
insertEmployee.setString(2, employee.getName());
insertEmployee.setString(3, employee.getDesignation());
insertEmployee.executeUpdate();
System.out.println("Created new Employee");
}
finally {
if (con != null)
con.close();
}
}
catch (SQLException e) {
System.out.println("Exception Occured while adding new Employee");
}
}
@Override
public List<Employee> getAllEmployees() {
List<Employee> results = new ArrayList<Employee>();
try {
Connection con = null;
PreparedStatement retrieveBooks = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:hsqldb:file:database.dat;shutdown=true", "sa", "");
retrieveBooks = con.prepareStatement("select * from Employee");
rs = retrieveBooks.executeQuery();
while (rs.next()) {
String empId = rs.getString(1);
String name = rs.getString(2);
String designation = rs.getString(3);
double salary = 0;
Employee emp = new Employee(empId, name, designation, salary);
results.add(emp);
}
}
finally {
if (rs != null)
rs.close();
if (con != null)
con.close();
}
}
catch (SQLException e) {
System.out.println("Exception Occured while retrieving employees");
}
return results;
}
}
Our EmployeeService.java will remain unchanged. The only difference will be that EmployeeDAO will now refer to theEmployeeDAO.java interface.
We will now modify the application.xml. Now the employeeDAO will refer to EmployeeDAOImplUsingJDBC.java
Now we are done with the coding changes. If we run the MainApplication.java we get the output as.

So to implement the Programming to interface in Spring have added the EmployeeDAO.java interface between the EmployeeService.java and the EmployeeDAOUsingJDBC.java. In the next chapter we will
add the EmployeeDAOUsingSpringJDBC.java. Now we will only have to make change to the application.xml
to use this DAO class.
What Next?
In the next chapter will implement Implement SpringJDBC.Popular Posts
1Z0-830 Java SE 21 Developer Certification
Azure AI Foundry Hello World
Azure AI Agent Hello World
Foundry vs Hub Projects
Build Agents with SDK
Bing Web Search Agent
Function Calling Agent
Spring Boot + Azure Key Vault Hello World Example
Spring Boot + Elasticsearch + Azure Key Vault Example
Spring Boot Azure AD (Entra ID) OAuth 2.0 Authentication
Deploy Spring Boot App to Azure App Service
Secure Azure App Service using Azure API Management
Deploy Spring Boot JAR to Azure App Service
Deploy Spring Boot + MySQL to Azure App Service
Spring Boot + Azure Managed Identity Example
Secure Spring Boot Azure Web App with Managed Identity + App Registration
Elasticsearch 8 Security - Integrate Azure AD OIDC