Results 1 to 3 of 3

Thread: One interface, one class: multiple beans

  1. #1
    Join Date
    May 2010
    Posts
    2

    Default 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. #2
    Join Date
    May 2010
    Posts
    2

    Default

    anyone can tell me something?

  3. #3
    Join Date
    May 2010
    Location
    Lima, Perú
    Posts
    26

    Default

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •