这是你如何在你的服务层呼叫你的Dao?

mrb*_*lah 0 java oop

在你的服务层,假设你有一个方法做XX,这是你如何引用你的Dao类?

public class SomeServiceImpl implements SomeService

    public void DoSomething(int someThingId){

    UserDao userDao = this.daoFactory().GetUserDao();
    SalesDao salesDao = this.daoFactory().GetSalesDao();
    ..
    ..
    ..

    }
Run Code Online (Sandbox Code Playgroud)

它有点多做这个,并希望是否有一个更容易更优雅的方式?

Ral*_*und 7

我使用Springframework来配置我的应用程序.这个框架有一个很好的功能,使我能够将依赖项注入我的服务层.因此,Service实现看起来像这样:

@Autowired
private UserDAO userDao;
public void doSomething(int someThingId) {
userDAO.findById(someThingId);
...
}
Run Code Online (Sandbox Code Playgroud)