Page tree
Skip to end of metadata
Go to start of metadata

About this Tutorial

This tutorial creates a Business Facade class (and a JUnit Test) to talk to the DAO you created in the Persistence Tutorial.

In the context of AppFuse, this is called a Manager class. Its main responsibility is to act as a bridge between the persistence (DAO) layer and the web layer. It's also useful for de-coupling your presentation layer from your database layer. Managers should also be where you put any business logic for your application.

You will learn two things:

  1. You don't need to write Managers if you just need generic CRUD functionality.
  2. How to write Managers when you need custom functionality.

Table of Contents

  1. Register a personManager bean definition
  2. Create a Manager Interface
  3. Create a Manager Test to test finder functionality
  4. Create a Manager Implementation
  5. Run the Manager Test
  6. Register your Manager Implementation
  7. Write the Manager Test using EasyMock

Source Code


The code for this tutorial is located in the "tutorial-service" module of the appfuse-demos project on GitHub. Use the following command to check it out from Subversion:

svn co

Register a personManager bean definition

AppFuse doesn't require you to write a Manager to persist a POJO. You can use the GenericManager class if all you need is CRUD on an object. To register a personManager bean, its best to wrap the personDao bean.



All service.*Manager beans will automatically be configured by Spring to wrap PROPOGATION_REQUIRED transactions around their method executions. This is done by the following Spring AOP configuration in appfuse-service.jar.

    <aop:advisor id="managerTx" advice-ref="txAdvice"          
        pointcut="execution(* *..service.*Manager.*(..))" order="2"/> 

Open your src/main/webapp/WEB-INF/applicationContext.xml and replace the personDao bean with the following.


JPA Configuration


For JPA, you'll need to change the nested DAO that gets injected into GenericManagerImpl:

  • <bean class="org.appfuse.dao.jpa.GenericDaoJpa" ...

If you wrote the PersonDao interface and implementation in the previous tutorial, you'll want to use the following for your personManager bean definition. If you don't, your PersonDaoTest will fail because there's no longer an exposed personDao bean.

Once you've created a personManager bean definition, you can use this bean on an object by adding the following setter method:

The Spring Framework


To learn more about how Spring works, please see the Spring Reference Guide.

That's it! To persist an object with AppFuse, all you need to do is:

  1. Create a POJO with annotations.
  2. Register it with your persistence framework.
  3. Write some XML to register a type-safe class to persist it.

Not only did you write data access code, you also used interfaces to abstract your implementation. This means that it should be possible to replace your persistence framework at any time.


You can also perform all 3 steps with the AppFuse Maven Plugin.

If you need more than just CRUD functionality, you'll want to continue reading below. From here, you can expose your manager as a web service or continue writing your web application.

Create a Manager Interface

First off, create a PersonManager interface (in the src/main/java/**/service directory - you may need to create this first) and specify the finder method for any implementation classes.

Create a Manager Test to test finder functionality

Now you know what you want your manager to do, so it's time to write the tests. In this example, you will use mock objects to isolate your tests from external dependencies. This means you do not need a database set up to run these tests, and it will be easier to test different scenarios. In a large project, you could even test your Manager class before someone else finished writing the DAO implementation.

You can write your own mock objects, but here you are going to use MockitoCreate PersonManagerImplTest in src/test/java/**/service/impl:

This will not compile, as you have not created the PersonManagerImpl class it tests.

Create a Manager Implementation

The next step is to create a PersonManagerImpl class that implements the methods in PersonManager.

Create in src/main/java/**/service/impl.

Now before you run your tests, review your test class to make sure that it will test all possible conditions.

Run the Manager Test

Save all your edited files and try running mvn test -Dtest=PersonManagerImplTest.

Register your Manager Implementation

There are two ways to register your personManager bean at this point: 1) using a @Service annotation or 2) with XML. To use annotations, simply add @Service("personManager") as a class level parameter:


If you registered the personDao using the @Repository("personDao") annotation instead of src/main/webapp/WEB-INF/applicationContext.xml file you will need to auto-wire the PersonDao variable in the PersonManagerImpl class:

public PersonManagerImpl(PersonDao personDao) {
    this.personDao = personDao;

If you'd prefer to use XML, open your src/main/webapp/WEB-INF/applicationContext.xml file and replace the personManager bean with the following:


The Web application tutorials assume that you will be using the GenericManager. If you follow them after making this change, you will need to change all the references in their code from the GenericManager to your new PersonManager interface. For example:

private PersonManager personManager;

That's it. If you want to see how to use EasyMock instead of Mockito, then carry on reading. If not, you can expose your manager as a web service or continue to writing your web application.

Using EasyMock instead of Mockito in Unit Tests

Sooner or later, you will want to add extra dependencies into your project.

AppFuse ships with Mockito, but not EasyMock, so this section will show you how to add EasyMock as a project dependency.

Edit your pom.xml file in your project's top level directory. Add EasyMock as a dependency in the <dependencies> element:

EasyMock is only required during testing, and you don't want it packaged in your application, so it's restricted it to a scope of test.


If you remove Mockito and add EasyMock to your pom.xml, you'll want to refresh your project in your IDE.

Edit the PersonManagerImplTest class you wrote above so it looks as follows:

Now check everything works by running mvn test -Dtest=PersonManagerImplTest again.

From here, you can expose your manager as a web service or continue writing your web application.