[英]No qualifying bean of type in Spring 4
Currently I'm facing an issue in Autowire configuration between controller and the service layer. 目前,我在控制器和服务层之间的Autowire配置中遇到问题。
Log 日志记录
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 控制者
@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 服务
@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();
}
}
Autowiring doesnt work for some reason... " No qualifying bean of type com.ults.hrms.service.EmployeeService
. 由于某些原因,自动装配不起作用...“
No qualifying bean of type com.ults.hrms.service.EmployeeService
。
I've tried with different combinations of @Component
and @Transactional
too. 我也尝试了
@Component
和@Transactional
不同组合。
Sorry for a lot of code, but I don't know what can cause that error anymore. 抱歉,有很多代码,但是我不知道是什么导致了该错误。
The root cause of No qualifying bean of type [com.ults.hrms.service.EmployeeService]
is that the autowired property name is not correct.. No qualifying bean of type [com.ults.hrms.service.EmployeeService]
的根本原因是自动装配的属性名称不正确。
change 更改
@Autowired
EmployeeService employeService;
to 至
@Autowired
EmployeeService employeeService ;
You can do like this 你可以这样
@Service("employeService")
public class EmployeeService extends GenericDaoImpl<Employe>{
public List<Employe> findAllUsers() throws Exception {
return findAllUsers();
}
}
@Service***("employeService")*** this allows you to choose the name you are going to use while autowiring @Service ***(“ employeService”)***这使您可以选择自动装配时要使用的名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.