who needs implementations?

At least for some common patterns like DAOs.

As a popular manner, most of the java applications have at least three layers like UI, Service and DAO. For each layer you probably have interface and a single or more implementation classes.  And while UI layer has some really complex businesses for effective UI experience, DAO layer is only related with CRUD operations for a single or a group of beans.

Here is the routine that you can get rid of


public interface UserElementDAO {
public User create(String username,String password,String email);
public User updatePassword(String username,String password);
public User updateEmail(String username,String email);
public List<User> findUsers(String email);


After  this implementation there is no need to give an extra information for developer. But you still need the implementation ? No not necessary here is the RECIPE : more Annotation and BeanFactory

Let’s give some more BINARY information (annotation)

public interface UserElementDAO {
public User create(
@DAOMethodParameter(property="username")String username,
@DAOMethodParameter(property="password")String password,
@DAOMethodParameter(property="email")String email);

public User updatePassword(
@DAOMethodParameter(property="username")String username,
@DAOMethodParameter(property="password")String password);

public User updateEmail(
@DAOMethodParameter(property="username")String username,
@DAOMethodParameter(property="email")String email);


public List<User> findUsers(
@DAOMethodParameter(property = "email",operator=DAOMethodOperator.LIKE)String email);


After this point what you will do is to generate a dynamic Proxy with the help of spring BeanFactory

and inject it where it is necesary.

Example implementation for hibernate:

public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy)

throws Throwable {
String name = method.getName();

// first check if it a an implementation
if (name.equals("toString")) return targetDAOImpl.getClass().getName() +
" Proxy";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("method:" + name);
LOGGER.debug("args = " + Arrays.asList(args));
if (Modifier.isAbstract(method.getModifiers())) {
DAOMethod annotation = method.getAnnotation(DAOMethod.class);
LOGGER.info("GenericHibernateDAOFactory.no handler"+method.getName());
return null;

// method is abstract choose method handler
return saveMethod(obj,method, args,proxy);
}else if(annotation.type().equals(DAOMethodType.UPDATE)){
return updateMethod(obj,method, args,proxy);
}else if(annotation.type().equals(DAOMethodType.DELETE)){
return deleteMethod(obj,method, args,proxy);
}else if(annotation.type().equals(DAOMethodType.SEARCH)){
return searchMethod(obj,method, args,proxy);
}else if(annotation.type().equals(DAOMethodType.COUNT)){
return countMethod(obj,method, args,proxy);

return proxy.invokeSuper(genericHibernateDAO, args);

private Object saveMethod(Object obj, Method method, Object[] args,
MethodProxy proxy)

throws      InstantiationException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Object o = objectClass.newInstance();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterAnnotations.length; i++) {
Annotation[] parameterAnnotation = parameterAnnotations[i];
if(parameterAnnotation ==null)continue;
for (Annotation annotation1 : parameterAnnotation) {
if (annotation1 instanceof DAOMethodParameter) {
DAOMethodParameter daoMethodParameter = (DAOMethodParameter) annotation1;
String s = daoMethodParameter.property();
PropertyUtils.setProperty(o, s, args[i]);
return o;

Any opinions or suggestions are always welcome :)

  • Trackback are closed
  • Comments (4)
    • Omar
    • October 3rd, 2008

    I was looking for something like a Generic Service Layer because in a recommended implementation we are using [recommended by an architect who has Framework fever].

    The recommendation consists of 4+ layers, The service layer was splitted in 2 additional layers, Business Service (BS) and Business Objects (BO), so this always lead us to have a robust BS and a forwarding BO layer. I think annotations could be a solution for not to break this “recommendation” but to save some development time.

    Personally I still don’t buy the fact that not to implement my DAO layer but it seems interesting, I’m going to give a try on this.

  1. Hi Omar,
    I do use such a frame for my own prototype application it is %90 sufficient for any ORM business and less code means less coding error :)

    i hopefully submit a proof of concept implementation as a small open source project

    good luck to you too

  2. Hi every body,
    actually here I want the discuss the idea and approach not the sample draft implementation :)

    all comments that would add value would welcome

    • ceren
    • August 27th, 2010

    How do you use the @Repository annotation with GenericDao implementations? You can not add the parent to UserElementDAO with annotations.

Comment are closed.