Spring 3 Service Dao 层与抽象类问题
我正在尝试在我的服务层中实现一个通用抽象类。我已经在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不必调用方法 (
setDao()
) 来允许子类将 DAO 引用传递给超类,为什么要反转方向并强制子类向超类提供 DAO?例如:
不需要从外部调用方法来启动引用传递链。
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:
There is no need to call a method externally to kick the reference-passing-chain into action.
尝试让您的 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.
将spring框架版本升级到4,问题就解决了。
检查此页面。
Upgrade spring framework version to 4, and the problem will be solved.
check this page.