spring注解

xiaoxiao2021-08-17  216

spring的注解 1、Qualifier 当项目启动报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeInfoControl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.test.service.EmployeeService com.test.controller.EmployeeInfoControl.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.test.service.EmployeeService] is defined: expected single matching bean but found 2: [service1, service2]

其实报错信息已经说得很明确了,在autoware时,由于有两个类实现了EmployeeService接口,所以Spring不知道应该绑定哪个实现类,所以抛出了如上错误。 有两种解决方案: 第一种在:Autowired的注释上添加Qualifier的注释,如下 @Autowired @Qualifier(“gsonSerializer”) 注:qualifier注释中的值,必须为bean中配置的 第二种:使用@Resource代替@Autowired; 原因如下: @Autowried 默认是按照类型进行装配注入,默认情况下,它要求依赖对象必须存在,如果允许 null 值,可以设置它 required 为false。 @Resource 默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来注入。 如果既不指定 name 属性又不指定 type 属性,Spring这时通过反射机制使用 byName 自动注入策略 2、定时任务@Scheduled https://blog.csdn.net/prisonbreak_/article/details/49180307 3、线程异步task @Async https://www.cnblogs.com/moxiaotao/p/9415304.html

转载请注明原文地址: https://www.6miu.com/read-4823486.html

最新回复(0)