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> { // ... }I thank you if there is a definitive solution.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; } }


Reply With Quote
