Spring 3 Service Dao 层与抽象类问题

发布于 2024-11-27 06:05:50 字数 2220 浏览 0 评论 0原文

我正在尝试在我的服务层中实现一个通用抽象类。我已经在我的 dao 层中使用了类似的模式,并且效果很好。我在 Spring in Practice v8 电子书中找到了一个有效的示例。我想知道是否有办法自动连接以下工作代码。 (代码有效,但在使用类中的任何其他方法之前,我必须调用我的帮助器方法“setDao”)

测试类:

    public class App {


    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");

        MyService service = (MyService)appContext.getBean("myService");

        service.setDao();

        Heading detail = new Heading();
        detail.setName("hello");

        service.save(detail);

        Heading dos = service.findById(Long.valueOf(1));
        System.out.println(dos);
    }
}

MyServiceImpl类

    @Service("myService")
public class MyServiceImpl extends AbstractServiceImpl<Heading> implements HeadingService {

    @Autowired
    private HeadingDao headingDao;

    public void setHeadingDao(HeadingDao headingDao) {
        this.headingDao = headingDao;
    }

    public void setDao() {
        super.setDao(this.headingDao);
    }

}

MyService接口

    public interface HeadingService extends AbstractService<Heading> {
    public void setDao();
}

AbstractServiceImpl类

    @Service
public abstract class AbstractServiceImpl<T extends Object> implements AbstractService<T> {

    private AbstractDao<T> dao;

    public void setDao(AbstractDao<T> dao) {
        this.dao = dao;
    }

    public void save(T t) {
        dao.save(t);
    }

    public T findById(Long id) {
        return (T)dao.findById(id);
    }

    public List<T> findAll() {
        return dao.findAll();
    }

    public void update(T t) {
        dao.update(t);
    }

    public void delete(T t) {
        dao.delete(t);
    }

    public long count() {
        return dao.count();
    }

}

AbstractService接口

    public interface AbstractService<T extends Object> {

    public void save(T t);
    public T findById(Long id);
    public List<T> findAll();
    public void update(T t);
    public void delete(T t);
    public long count();

}

I am trying to implement a generic abstract class in my service layer. I am already using a simliar pattern in my dao layer and it works fine. I found a working example in the Spring in Practice v8 ebook. I am wondering if there is a way to autowire the following working code. (The code works but I have to call my helper method 'setDao' before I use any other method in the class)

Test class:

    public class App {


    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");

        MyService service = (MyService)appContext.getBean("myService");

        service.setDao();

        Heading detail = new Heading();
        detail.setName("hello");

        service.save(detail);

        Heading dos = service.findById(Long.valueOf(1));
        System.out.println(dos);
    }
}

MyServiceImpl class

    @Service("myService")
public class MyServiceImpl extends AbstractServiceImpl<Heading> implements HeadingService {

    @Autowired
    private HeadingDao headingDao;

    public void setHeadingDao(HeadingDao headingDao) {
        this.headingDao = headingDao;
    }

    public void setDao() {
        super.setDao(this.headingDao);
    }

}

MyService interface

    public interface HeadingService extends AbstractService<Heading> {
    public void setDao();
}

AbstractServiceImpl class

    @Service
public abstract class AbstractServiceImpl<T extends Object> implements AbstractService<T> {

    private AbstractDao<T> dao;

    public void setDao(AbstractDao<T> dao) {
        this.dao = dao;
    }

    public void save(T t) {
        dao.save(t);
    }

    public T findById(Long id) {
        return (T)dao.findById(id);
    }

    public List<T> findAll() {
        return dao.findAll();
    }

    public void update(T t) {
        dao.update(t);
    }

    public void delete(T t) {
        dao.delete(t);
    }

    public long count() {
        return dao.count();
    }

}

AbstractService interface

    public interface AbstractService<T extends Object> {

    public void save(T t);
    public T findById(Long id);
    public List<T> findAll();
    public void update(T t);
    public void delete(T t);
    public long count();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强者自强 2024-12-04 06:05:50

不必调用方法 (setDao()) 来允许子类将 DAO 引用传递给超类,为什么要反转方向并强制子类向超类提供 DAO?

例如:

public abstract class AbstractServiceImpl<T extends Object> implements AbstractService<T> {
    private AbstractDao<T> dao;

    abstract AbstractDao<T> getDao();

    public void save(T t) {
        getDao().save(t);
    }
}

public class FooServiceImpl extends AbstractServiceImpl<Foo> {
     @Autowired
     private FooDao fooDao;

     @Overrides
     public AbstractDao<Foo> getDao() {
         return fooDao;
     }
}

不需要从外部调用方法来启动引用传递链。

Instead of having to call a method (setDao()) to allow your subclass to pass the DAO reference to your superclass, why reverse the direction and force the subclass to supply the DAO to the superclass?

for example:

public abstract class AbstractServiceImpl<T extends Object> implements AbstractService<T> {
    private AbstractDao<T> dao;

    abstract AbstractDao<T> getDao();

    public void save(T t) {
        getDao().save(t);
    }
}

public class FooServiceImpl extends AbstractServiceImpl<Foo> {
     @Autowired
     private FooDao fooDao;

     @Overrides
     public AbstractDao<Foo> getDao() {
         return fooDao;
     }
}

There is no need to call a method externally to kick the reference-passing-chain into action.

尝蛊 2024-12-04 06:05:50

尝试让您的 MyServiceImpl 实现 InitializingBean,并将 setDao() 方法更改为 afterPropertiesSet()。框架调用 setter 后,它将自动被调用。

或者,(更简单),只需在 setHeaderDao(...) 方法中调用 setDao() 即可。

Try making your MyServiceImpl implement InitializingBean, and change your setDao() method to be afterPropertiesSet(). It will automatically get called after the framework is done calling setters.

Or, (even more simple), just call setDao() in your setHeaderDao(...) method.

流年已逝 2024-12-04 06:05:50

将spring框架版本升级到4,问题就解决了。
检查页面。

Upgrade spring framework version to 4, and the problem will be solved.
check this page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文