Spring Java中许多DAO的策略 [英] Strategy for many DAOs in Spring Java
问题描述
我们在现有项目中有许多DAO(当前没有接口,但是可以更改).我们没有为每个DAO类连接一个Spring管理的Bean并将它们注入服务层,而是有一个DAO的工厂",其形式如下:
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();
}
...
(请注意,MyDAO1和MyDAO2是具体的类)
这使我们能够轻松地在Service层中添加/调用DAO方法,而不必1.)将DAO接口作为属性添加到服务类中; 2.)通过配置将DAO实现连接到服务方法中. (而且我们有时在一个服务类中使用多个DAO).
DAOFactory.getInstance().getMyDAO1().doSomething();
到目前为止,该策略对我们一直有效(我们并不需要太多切换实现),但是我想知道是否有更好的方法可以启动新的?我将DAO作为Bean进行自动装配,但是我仍然需要在每个服务类中创建属性以表示所使用的DAO.在一个大型项目中,无论如何我还是很犹豫是否开始自动装配bean-我们需要为所有开发人员提供可见性.
感觉就像我在a.)紧密耦合到一个实现,但是更少的代码/配置开销和b.)松散耦合到接口,但是需要大量的代码/配置开销之间进行翻转./p>
有没有更好的方法让我丢失?介于两者之间?欢迎意见.
我将把所有DAO都作为Spring托管的组件,并将它们注入服务中以进行松散耦合.为什么您认为在大型项目中自动装配bean不好??
只需使用@Component注释每个DAO类
并将MyDao mydao = factory.getmyDao()
替换为
@Autowired
MyDao myDao;
我没有看到太多的编码/配置开销.
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.
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.
这篇关于Spring Java中许多DAO的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!