6. Spring for Configuring Three-Tier Applications
Here, we will use an example to explore how Spring can be used to configure three-tier applications. A common example of this type of application is web applications.
6.1. The Problem
We want to build a three-tier application with the following structure:
![]() |
- the three layers will be made independent through the use of interfaces
- The integration of the three layers will be handled by Spring
- We will create separate packages for each of the three layers, which we will name [control], [domain], and [dao]. An additional package, [tests], will contain the test applications.
6.2. The data access layer
The DAO layer will implement the following interface:
Namespace istia.st.spring3tier.dao
Public Interface IDao
' do something in the [dao] layer
Function doSomethingInDaoLayer(ByVal a As Integer, ByVal b As Integer) As Integer
End Interface
End Namespace
- Write two classes, Dao1Impl1 and Dao1Impl2, that implement the IDao interface. The method Dao1Impl1.doSomethingInDaoLayer will return a+b, and the method Dao1Impl2.doSomethingInDaoLayer will return a-b.
- Write a test class NUnit that tests the two previous classes
6.3. The business layer
The business layer will implement the following interface:
Namespace istia.st.spring3tier.domain
Public Interface IDomain
' do something in the [domain] layer
Function doSomethingInDomainLayer(ByVal a As Integer, ByVal b As Integer) As Integer
End Interface
End Namespace
- Write two classes, Domain1Impl1 and Domain1Impl2, that implement the IDomain interface. These classes will have a private field of type IDao that can be initialized via a public property. The doSomethingInDomainLayer method of the [DomainImpl1] class will increment a and b by one, then pass these two parameters to the doSomethingInDaoLayer method of the received object of type IDao1. The doSomethingInDomainLayer method of the [Domain1Impl2elle] class will decrement a and b by one before doing the same thing.
- Write a test class NUnit that tests the two previous classes
6.4. The user interface layer
The user interface layer will implement the following interface:
Namespace istia.st.spring3tier.control
Public Interface IControl
' do something in the [control] layer
Function doSomethingInControlLayer(ByVal a As Integer, ByVal b As Integer) As Integer
End Interface
End Namespace
- Write two classes, Control1Impl1 and Control1Impl2, that implement the IContro1 interface. These classes will have a private field of type IDomain that can be initialized via a public property. The doSomethingInControlLayer method of the [Control1Impl1] class will increment a and b by one, then pass these two parameters to the doSomethingInDomainLayer method of the received object of type IDomain1. The doSomethingInControlLayer method of the [Control11Impl2] class, on the other hand, will decrement a and b by one before doing the same thing.
- Write a test class NUnit to test the two previous classes
6.5. Integration with Spring
- Write a Spring configuration file that will determine which classes each of the three previous layers should use
- Write a test class NUnit using different Spring configurations to highlight the flexibility of the application
- Write a standalone application (main method) that passes two parameters to the [doSomethingInControlLayer] method of the implemented IControl interface and displays the result rendered by the interface.
