Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy for many DAOs in Spring Java

Tags:

java

spring

dao

We have many DAOs in an existing project (currently with no interfaces, but that can change). Rather than wiring a Spring-managed bean for each DAO class and injecting them into the service layer, we have a DAO "factory" of sorts that looks like this:

public class DAOFactory {
private static DAOFactory daoFac;

static{
    daoFac = new DAOFactory();
}

private DAOFactory(){}

public static DAOFactory getInstance(){
    return daoFac;
}

public MyDAO1 getMyDAO1(){
    return new MyDAO1();
}

    public MyDAO2 getMyDAO2(){
    return new MyDAO2();
}
    ...

(Note that MyDAO1 and MyDAO2 are concrete classes)

This allows us to easily add/call DAO methods within the Service layer, without having to 1.) add a DAO interface as a property to the service class 2.) wire the DAO implementation into service method via configuration. (And we sometimes use multiple DAOs in one service class).

DAOFactory.getInstance().getMyDAO1().doSomething();

This strategy has worked for us so far (we haven't had much need for switching implementations), but I'm wondering if there is a better method if we were able to start new? I looked at autowiring the DAOs as beans, but I'd still need to create properties in each service class to represent those DAOs being used. And in a large project, I'm hesitant to start auto-wiring beans anyway - we need to provide visibility for all developers.

It feels like I'm flip-flopping between a.) being tightly-coupled to an implementation, but less code/config overhead and b.) being loosely coupled to interfaces, but requiring plenty of code/configuration overhead.

Is there a better way I'm missing? Something in-between? Opinions welcomed.

like image 945
Ryan H Avatar asked Dec 06 '12 18:12

Ryan H


People also ask

Can you use @service over a DAO?

In your scenario, it has no impact on application flow whether you use @Service or @Repository, application will work as they are elligible for autowiring. But standard practice is to use @Repository for dao classes.

Can DAO use another DAO?

This data would commonly be accessed together, so better to have a single DAO grouping/querying the business objects. DAOs can call other DAOs, but depending on the scope, it's probably better to move the logic of that up a level to the application service.

Why we use DAO class in Spring?

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate, JPA or JDO in a consistent way.

What is known as DAO interface in Spring?

DAO stands for Data Access Object. It's a design pattern in which a data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanisms.


1 Answers

I will have all the DAO s as Spring managed components and inject them into services for loose coupling. Why do you think autowiring beans is bad in a big project.?

Just annotate each DAO class with @Component and replace MyDao mydao = factory.getmyDao() with

@Autowired MyDao myDao;

I dont see much coding/configuration overhead with it.

like image 114
Subin Sebastian Avatar answered Oct 25 '22 00:10

Subin Sebastian