Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring NoSuchBeanDefinitionException:没有类型匹配的bean_Spring_Junit_Annotations - Fatal编程技术网

Spring NoSuchBeanDefinitionException:没有类型匹配的bean

Spring NoSuchBeanDefinitionException:没有类型匹配的bean,spring,junit,annotations,Spring,Junit,Annotations,我正在尝试一些代码。 它是一个架构Hibernate-JPA-Spring。现在,我希望在JUnit测试中运行它 目前,我有一些例外: GRAVE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6295eb] to prepare test instance [test

我正在尝试一些代码。 它是一个架构Hibernate-JPA-Spring。现在,我希望在JUnit测试中运行它

目前,我有一些例外:

GRAVE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6295eb] to prepare test instance [test.service.UserAccountServiceTest@609959]
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);
    }
}

}
<!-- 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" />
import java.util.List;
导入org.apache.commons.logging.Log;
导入org.apache.commons.logging.LogFactory;
导入org.junit.After;
导入org.junit.AfterClass;
导入org.junit.Before;
导入org.junit.BeforeClass;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.test.context.ContextConfiguration;
导入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
导入org.springframework.util.Assert;
导入test.jndi.ContextDatasourceCreator;
导入test.persistence.entity.UserAccount;
@RunWith(SpringJUnit4ClassRunner.class)
@上下文配置(位置={
“classpath:/spring/applicationContext.xml”})
公共类UserAccountServiceTest扩展了断言{
私有静态最终日志日志=LogFactory.getFactory().getInstance(UserAccountServiceTest.class);
@自动连线
@限定符(“userAccountService”)
私人用户帐户服务用户帐户服务;
@课前
公共静态void setUpOnce(){
LOG.info(“setUpOnce()”);
ContextDatasourceCreator.init();
}
@下课
公共静态void tearDownOnce(){
LOG.info(“tearDownOnce()”;
}
@以前
设置时的公共无效(){
LOG.info(“onSetUp()”);
}
@之后
公共资产减值(){
LOG.info(“OnTearDown()”);
}
@试验
public void testListAll(){
List allUserAccounts=userAccountService.getAllAccounts();
对于(用户帐户用户帐户:AlluseAccounts){
日志信息(用户帐户);
}
}
}
/这里是我的应用程序上下文/

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);
    }
}

}
<!-- 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" />

/这里是我的源代码/

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);
    }
}

}
<!-- 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" />
通用DAO接口:

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);
}
import test.persistence.entity.UserAccount;

public interface UserAccountDao extends GenericDao<UserAccount> {

UserAccount findAccount(String matricule);
}
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;
}

}
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();

}
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);
}

}
import java.util.List;
公共接口通用DAO{
T保存(T pojo);
无效删除(类别E,内部id);
无效删除(T pojo);
T findById(类e,int-id);
清单findAll(E类);
列表findByQuery(字符串jpql);
}
Dao接口:

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);
}
import test.persistence.entity.UserAccount;

public interface UserAccountDao extends GenericDao<UserAccount> {

UserAccount findAccount(String matricule);
}
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;
}

}
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();

}
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);
}

}
导入test.persistence.entity.UserAccount;
公共接口UserAccountDao扩展了GenericDao{
UserAccount findAccount(字符串矩阵);
}
通用DAO Impl:

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);
}
import test.persistence.entity.UserAccount;

public interface UserAccountDao extends GenericDao<UserAccount> {

UserAccount findAccount(String matricule);
}
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;
}

}
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();

}
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);
}

}
import java.util.List;
导入javax.persistence.EntityManager;
导入javax.persistence.PersistenceContext;
导入test.persistence.dao.GenericDao;
公共抽象类GenericDaoImpl实现GenericDao{
@持久上下文
受保护的实体管理器em;
公共T保存(T pojo){
返回em.merge(pojo);
}
公共无效删除(类别E,内部id){
T pojo=findById(classe,id);
if(pojo!=null){
em.remove(pojo);
}
}
公共作废删除(T pojo){
em.remove(pojo);
}
公共T findById(类classe,int-id){
返回(T)em.find(classe,id);
}
公共列表findAll(E类){
StringBuffer jpql=新的StringBuffer(20);
append(“from”).append(classe.getName());
List result=em.createQuery(jpql.toString()).getResultList();
返回结果;
}
公共列表findByQuery(字符串jpql){
List result=em.createQuery(jpql).getResultList();
返回结果;
}
}
Dao Impl:

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);
}
import test.persistence.entity.UserAccount;

public interface UserAccountDao extends GenericDao<UserAccount> {

UserAccount findAccount(String matricule);
}
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;
}

}
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();

}
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);
}

}
import javax.persistence.NoResultException;
导入javax.persistence.Query;
导入org.springframework.stereotype.Repository;
导入test.persistence.dao.UserAccountDao;
导入test.persistence.entity.UserAccount;
@存储库(“userAccountDao”)
公共类UserAccountDaoImpl扩展了GenericDaoImpl实现UserAccountDao{
公共用户帐户findAccount(字符串矩阵){
Query Query=em.createNamedQuery(“UserAccount.login”);
query.setParameter(“矩阵”,矩阵);
UserAccount=null;
试一试{
account=(UserAccount)query.getSingleResult();
}捕获(NoResultException nre){
}
返回帐户;
}
}
服务接口:

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);
}
import test.persistence.entity.UserAccount;

public interface UserAccountDao extends GenericDao<UserAccount> {

UserAccount findAccount(String matricule);
}
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;
}

}
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();

}
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);
}

}
import java.util.List;
导入test.persistence.entity.UserAccount;
公共接口UserAccountService{
公共抽象用户帐户登录(字符串矩阵);
公共抽象用户帐户寄存器(字符串矩阵);
公共抽象用户帐户getAccountWithId(整数id);
公共摘要列表getAllAccounts();
}
服务实施:

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);
}
import test.persistence.entity.UserAccount;

public interface UserAccountDao extends GenericDao<UserAccount> {

UserAccount findAccount(String matricule);
}
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;
}

}
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();

}
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);
}

}
import java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.stereotype.Service;
导入org.springframework.transaction.annotation.Transactional;
导入test.persistence.dao.UserAccountDao;
导入test.persistence.entity.UserAccount;
导入test.service.UserAccountService;
@服务(“用户帐户服务”)
@交易的
公共类UserAccountServiceImpl实现UserAccountService{
@自动连线
@限定符(“userAccountDao”)
私有UserAccountDao UserAccountDao;
公共用户帐户getAccountWithId(整数id){
返回userAccountDao.findById(UserAccount.class,id);
}
公共用户帐户登录(字符串矩阵){
返回userAccountDao.findAccount(矩阵);
}
公共用户帐户寄存器(字符串矩阵){
UserAccount=新的UserAccount();
账户设置矩阵(矩阵);
试一试{
account=userAccountDao.save(account);
}捕获(例外e){
}
返回帐户;
}
公共列表getAllAccounts(){
返回userAccountDao.findAll(UserAccount.class);
}
}
有什么想法吗?
非常感谢

我没有找到解决办法。 在Maven,它不起作用


在动态web项目中,如果我将@PersistenceContext更改为扩展的,我将成功执行测试。

尝试将
@Qualifier
添加到userAccountDaoImpl<代码>@Repository(“userAccountDao”)@限定符(“使用