0
我正在尝试一些代码。 它是一个体系结构Hibernate - JPA - Spring。目前,我希望在JUnit测试中运行它。NoSuchBeanDefinitionException:没有匹配的类型的bean
对于那一刻,我有一些例外:
GRAVE: Caught exception while allowing TestExecutionListener [org.springframewor[email protected]6295eb] to prepare test instance [[email protected]]
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAccountService': Injection of autowired dependencies failed;
...
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private test.persistence.dao.UserAccountDao test.service.impl.UserAccountServiceImpl.userAccountDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [test.persistence.dao.UserAccountDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [test.persistence.dao.UserAccountDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
... 43 more
20 juil. 2012 10:56:19 test.service.UserAccountServiceTest tearDownOnce
INFO: tearDownOnce()
这里了JUnit:UserServiceTest
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
import test.jndi.ContextDatasourceCreator;
import test.persistence.entity.UserAccount;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:/spring/applicationContext.xml"})
public class UserAccountServiceTest extends Assert {
private static final Log LOG = LogFactory.getFactory().getInstance(UserAccountServiceTest.class);
@Autowired
@Qualifier("userAccountService")
private UserAccountService userAccountService;
@BeforeClass
public static void setUpOnce() {
LOG.info("setUpOnce()");
ContextDatasourceCreator.init();
}
@AfterClass
public static void tearDownOnce() {
LOG.info("tearDownOnce()");
}
@Before
public void onSetUp() {
LOG.info("onSetUp()");
}
@After
public void OnTearDown() {
LOG.info("OnTearDown()");
}
@Test
public void testListAll() {
List<UserAccount> allUserAccounts = userAccountService.getAllAccounts();
for (UserAccount userAccount : allUserAccounts) {
LOG.info(userAccount);
}
}
}
/这里我的applicationContext /
<!-- Annotations Scan -->
<context:annotation-config/>
<context:component-scan base-package="test.service, test.persistence" />
<!-- Entity Manager Factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="dbrefPU" />
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Transaction Annotations -->
<tx:annotation-driven proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager" />
/这里我的源代码/
GenericDao接口:
import java.util.List;
public interface GenericDao<T extends Object> {
T save(T pojo);
void remove(Class<T> classe, int id);
void delete(T pojo);
T findById(Class<T> classe, int id);
List<T> findAll(Class<T> classe);
List<T> findByQuery(String jpql);
}
DAO接口:
import test.persistence.entity.UserAccount;
public interface UserAccountDao extends GenericDao<UserAccount> {
UserAccount findAccount(String matricule);
}
GenericDao默认地将Impl:
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import test.persistence.dao.GenericDao;
public abstract class GenericDaoImpl<T extends Object> implements GenericDao<T> {
@PersistenceContext
protected EntityManager em;
public T save(T pojo) {
return em.merge(pojo);
}
public void remove(Class<T> classe, int id) {
T pojo = findById(classe, id);
if (pojo != null) {
em.remove(pojo);
}
}
public void delete(T pojo) {
em.remove(pojo);
}
public T findById(Class<T> classe, int id) {
return (T) em.find(classe, id);
}
public List<T> findAll(Class<T> classe) {
StringBuffer jpql = new StringBuffer(20);
jpql.append("from ").append(classe.getName());
List<T> result = em.createQuery(jpql.toString()).getResultList();
return result;
}
public List<T> findByQuery(String jpql) {
List<T> result = em.createQuery(jpql).getResultList();
return result;
}
}
道默认地将Impl:
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import test.persistence.dao.UserAccountDao;
import test.persistence.entity.UserAccount;
@Repository("userAccountDao")
public class UserAccountDaoImpl extends GenericDaoImpl<UserAccount> implements UserAccountDao {
public UserAccount findAccount(String matricule) {
Query query = em.createNamedQuery("UserAccount.login");
query.setParameter("matricule", matricule);
UserAccount account = null;
try {
account = (UserAccount) query.getSingleResult();
} catch (NoResultException nre) {
}
return account;
}
}
服务接口:
import java.util.List;
import test.persistence.entity.UserAccount;
public interface UserAccountService {
public abstract UserAccount login(String matricule);
public abstract UserAccount register(String matricule);
public abstract UserAccount getAccountWithId(Integer id);
public abstract List<UserAccount> getAllAccounts();
}
服务默认地将Impl:
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import test.persistence.dao.UserAccountDao;
import test.persistence.entity.UserAccount;
import test.service.UserAccountService;
@Service("userAccountService")
@Transactional
public class UserAccountServiceImpl implements UserAccountService {
@Autowired
@Qualifier("userAccountDao")
private UserAccountDao userAccountDao;
public UserAccount getAccountWithId(Integer id) {
return userAccountDao.findById(UserAccount.class, id);
}
public UserAccount login(String matricule) {
return userAccountDao.findAccount(matricule);
}
public UserAccount register(String matricule) {
UserAccount account = new UserAccount();
account.setMatricule(matricule);
try {
account = userAccountDao.save(account);
} catch (Exception e) {
}
return account;
}
public List<UserAccount> getAllAccounts() {
return userAccountDao.findAll(UserAccount.class);
}
}
有什么想法吗? 非常感谢!
尝试在userAccountDaoImpl中添加“@ Qualifier”。 '@Repository(“userAccountDao”)@Qualifier(“userAccountDao”) public class UserAccountDaoImpl extends GenericDaoImpl..' – xyz 2012-07-20 10:57:12
没有任何效果,仍然产生异常。 – MychaL 2012-07-20 13:14:16
我不知道为什么,但如果我在经典的java web项目中使用这些源代码,它就可以工作。 pb是maven ....但是即使我们在WEB-INF/lib中注入依赖关系,这也是非常奇怪的,因为这不起作用。 有什么想法? 谢谢 – MychaL 2012-07-24 10:16:52