Announcement Announcement Module
Collapse
No announcement yet.
One interface, one class: multiple beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • One interface, one class: multiple beans

    Excuse me if there is already a topic about this but I could not find.

    I was using spring 2.5 configured by xml and I'm migrating to annotation.

    I have a interface DAO where I defined some methods for database access.

    And I have a unique implementation called GenericJpaDAOImpl. This implementation have some features that reduce the needing of writing queries, but I need to tell it which entity it is working.

    That was my previous configuration:

    Code:
    <bean id="productDAO" class="com.xxx.dao.impl.GenericJpaDAOImpl">
        <constructor-arg value="com.xxx.entities.Product" />
    </bean>
    
    <bean id="userDAO" class="com.xxx.dao.impl.GenericJpaDAOImpl">
        <constructor-arg value="com.xxx.entities.User" />
    </bean>
    Code:
    public class RequestCaseService {
        private DAO<Product, Long> productDAO;
        private DAO<User, Long> userDAO;
    
        public void setProductDAO(DAO<Product, Long> productDAO) {
            this.productDAO = productDAO;
        }
       
        public void setUserDAO(DAO<User, Long> userDAO) {
            this.userDAO= userDAO;
        }
       
    }

    I found a solution, but it's not a good and productive solution and I consider it a makeshift solution.

    Code:
    @Repository("dao")
    @Scope("prototype")
    public class GenericJpaDAOImpl<E,ID> implements DAO<E,ID> {
        // ...
    }
    Code:
    @Service
    public class RequestCaseService {
        private DAO<Product, Long> productDAO;
        private DAO<User, Long> userDAO;
    
        @Autowired
        public void setProductDAO(DAO<Product, Long> productDAO) {
            productDAO.setEntityClass(Product.class);
            this.productDAO = productDAO;
        }
       
        @Autowired
        public void setUserDAO(DAO<User, Long> userDAO) {
            userDAO.setEntityClass(User.class);
            this.userDAO= userDAO;
        }
       
    }
    I thank you if there is a definitive solution.

  • #2
    anyone can tell me something?

    Comment


    • #3
      Hi,
      In your case maybe the best option is to configure your: "productDAO" and "userDAO" in xml and only doing el autowiring with annotations.

      It means that you mantain:

      <bean id="productDAO" class="com.xxx.dao.impl.GenericJpaDAOImpl">
      <constructor-arg value="com.xxx.entities.Product" />
      </bean>

      <bean id="userDAO" class="com.xxx.dao.impl.GenericJpaDAOImpl">
      <constructor-arg value="com.xxx.entities.User" />
      </bean>

      and in the service do the autowiring:

      @Service
      public class RequestCaseService {
      @Autowired
      @Qualifier("productDAO")
      private DAO<Product, Long> productDAO;
      @Autowired
      @Qualifier("userDAO")
      private DAO<User, Long> userDAO;

      With these option your settings in the service are not needed.

      Regards

      Comment

      Working...
      X