I would like to use two different implementations for a DAO with Spring’s testframework.
src.main.java
.businessobjects
\-User.java
.dao
\-IUserDAO.java
.daojpa
\-UserDAO.java
.daohibernate
\-UserDAO.java
The spring testcase in:
src.test.java.base:
package base;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/hibernate-beans.xml")
@Transactional
public abstract class SpringTestCase {}
And here is the error:
Caused by:
java.lang.IllegalStateException:
Annotation-specified bean name
‘userDAO’ for bean class
[jpadao.UserDAO]
conflicts with existing,
non-compatible bean definition of same
name and class
[jpaadao.UserDAO]
I have already tried to override the autowiring by using qualifiers, e.g.:
<bean class="jpaadao.UserDAO">
<qualifier value="jpaa"/>
</bean>
<bean class="jpadao.UserDAO">
<qualifier value="jpa"/>
</bean>
And then in the testcase wiring with
@Autowired
@Qualifier("jpa")
private IUserDAO userDAO;
but the error persists.
Two questions:
- How can this problem be solved with annotation based configuration?
- How can I run tests WITHOUT autowiring and annotations?
You’re using beans without names so that Spring will try to come up with a name, this name may be based on the @Component annotation that you presumably have on your class, but it could also be the camelcased version of your the unqualified class name of your bean (in both cases they would turn out equal and that causes Spring to object).
Also, it seems you are mixing component scanning and xml configuration in a way that looks a bit odd to me.
There are many ways out of this, but most cleanly you would use only a single bean implementing the contract you’re trying to fullfil. If you do need different implementations you should give them different and more discriptive names:
This will give you more useful logging, even if the bean names are never used because you rely on auto wiring.