Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:03:38+00:00 2026-05-24T09:03:38+00:00

I am trying to implement a generic abstract class in my service layer. I

  • 0

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();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:03:40+00:00Added an answer on May 24, 2026 at 9:03 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement an identity map using generics. I have an abstract class,
I am trying to implement a generic Wrapper-Class for Qt's class system using C#'s
I have a generic class that I'm trying to implement implicit type casting for.
I'm trying to implement generic method to put in a class a calculated value
Am trying to implement a generic way for reading sections from a config file.
trying to implement a dialog-box style behaviour using a separate div section with all
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I'm trying to implement in Scala a generic data type parameterized on a type
I'm struggling with some Generic constraint issues when trying to implement a library that
I'm trying to implement a Generic Repository. This is what I've got so far

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.