サービス層に汎用抽象クラスを実装しようとしています。私はすでにdaoレイヤーで同様のパターンを使用しており、正常に機能します。Spring inPracticev8の電子ブックで実用的な例を見つけました。次の動作するコードを自動配線する方法があるかどうか疑問に思っています。(コードは機能しますが、クラス内の他のメソッドを使用する前に、ヘルパーメソッド'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();
}