目前我在控制器和服务层之间的Autowire配置中遇到了问题。
日志
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ults.hrms.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
控制器
@Controller
@RequestMapping("/")
public class AppController extends ConvertReqDataToJSON {
final static Logger logger = Logger.getLogger(AppController.class);
/* creating bean definition in Spring container(DI) */
@Autowired
EmployeeService employeService;
@RequestMapping(value = { "/listuser" }, method = RequestMethod.GET)
public @ResponseBody List<Employe> listUsers() throws Exception {
return employeService.findAllUsers();
}
}
服务
@Service
public class EmployeeService extends GenericDaoImpl<Employe>{
public List<Employe> findAllUsers() throws Exception {
return findAllUsers();
}
}
GenericDao
@SuppressWarnings("unchecked")
@Transactional
@Repository
public abstract class GenericDaoImpl<T> implements IGenericDao<T> {
protected Class<T> entityClass;
@Autowired
private SessionFactory sessionFactory;
public T findAll() throws Exception{
Criteria criteria =sessionFactory.getCurrentSession().createCriteria(getEntityClass()).addOrder(Order.asc("firstName"));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return (T) criteria.list();
}
}
自动装配由于某种原因无效......“No qualifying bean of type com.ults.hrms.service.EmployeeService
。
我也试过了@Component
和@Transactional
的不同组合。
很抱歉有很多代码,但我不知道会导致该错误的原因。
答案 0 :(得分:1)
No qualifying bean of type [com.ults.hrms.service.EmployeeService]
的根本原因是autowired属性名称不正确..
更改
@Autowired
EmployeeService employeService;
到
@Autowired
EmployeeService employeeService ;
答案 1 :(得分:0)
你可以这样做
@Service("employeService")
public class EmployeeService extends GenericDaoImpl<Employe>{
public List<Employe> findAllUsers() throws Exception {
return findAllUsers();
}
}
@Service ***(&#34; employeService&#34;)***这允许您选择在自动装配时使用的名称