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