Skip to content

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 call [control], [domain], and [dao]. An additional [tests] package 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 Dao1Impl1.doSomethingInDaoLayer method will return a+b, and the Dao1Impl2.doSomethingInDaoLayer method will return a-b.
  • Write a NUnit test class to test 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 IDao1 object. The doSomethingInDomainLayer method of the [Domain1Impl2] class will decrement a and b by one before doing the same thing.
  • Write a NUnit test class to test 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 IControl1 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 IDomain1 object. The doSomethingInControlLayer method of the [Control1Impl2] class, on the other hand, will decrement a and b by one before doing the same thing.
  • Write a NUnit test class 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 NUnit test class 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 returned by the interface.