springboot默认按照包顺序注入,注意Bean注入失败

本文介绍了一种常见的SpringBoot注入失败的问题及解决方法。通过在控制器中添加@ComponentScan注解来确保服务层能够被正确地扫描和注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

修改controller层代码

@Controller
@EnableAutoConfiguration
public class TestBootController {
   @Autowired
   private TestInterFace testInterFace;

   @RequestMapping("/num")
   @ResponseBody
   int home() {
   int i = testInterFace.testInterFace();
   return i;
   }
   @RequestMapping("/get")
   @ResponseBody User getUser() {
   return testInterFace.testUser();
   }

   public static void main(String[] args) throws Exception {
   SpringApplication.run(TestBootController.class, args);
   }

}

启动后抛异常

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘testBootController’: Unsatisfied dependency expressed through field ‘testInterFace’: No qualifying bean of type [com.kx.springboot.service.TestInterFace] found for dependency [com.kx.springboot.service.TestInterFace]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kx.springboot.service.TestInterFace] found for dependency [com.kx.springboot.service.TestInterFace]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘testBootController’: Unsatisfied dependency expressed through field ‘testInterFace’: No qualifying bean of type [com.kx.springboot.service.TestInterFace] found for dependency [com.kx.springboot.service.TestInterFace]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kx.springboot.service.TestInterFace] found for dependency [com.kx.springboot.service.TestInterFace]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

相信大家对这种异常非常常见,注入失败,这时候问题来了,按照传统的方式,对注入进行检查。service层已经加了@service的注解,怎么还是注入失败了呢,想查看配置文件,发现springboot没有配置文件,那么他是怎么扫描注解的呢?

 

百度了一下才知道,springboot默认按照包顺序注入,所以在创建controller时service还没有注入,springboot不需要传统的xml配置扫描包,只需要添加注解@ComponentScan(basePackages={“com.kx.springboot.service”}),代码如下

@Controller
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.kx.springboot.service"})//添加的注解
public class TestBootController {
   @Autowired
   private TestInterFace testInterFace;

   @RequestMapping("/num")
   @ResponseBody
   int home() {
   int i = testInterFace.testInterFace();
   return i;
   }
   @RequestMapping("/get")
   @ResponseBody User getUser() {
   return testInterFace.testUser();
   }

   public static void main(String[] args) throws Exception {
   SpringApplication.run(TestBootController.class, args);
   }

}

 

### Spring Boot 中 Impl 实现类 `@Autowired` 注入失败的原因分析 在Spring Boot项目中,当遇到Impl实现类里的`@Autowired`注入失败的情况时,这通常是由于配置不当或组件扫描机制未能正确识别待注入Bean所引起的。具体原因可能涉及多个方面: #### 1. 缺少必要的注解声明 如果服务层(Service Layer)的实现类缺少了`@Service`注解,则即使其他地方已经标注了`@ComponentScan`来指定路径,Spring仍然不会将其视为可管理的Bean实例,从而导致依赖项无法成功注入[^3]。 ```java // 正确的做法是在ServiceImpl加上@Service注解 @Slf4j @Service // 添加此行以确保被Spring容器管理 public class AppSendMsgServiceImpl implements ISendMsgService { @Autowired private NoticeInfoServiceImpl noticeInfoService; @Override public boolean sendMsg(Object object){ return noticeInfoService.send((NoticeRequest)object); } } ``` #### 2. Bean定义冲突 另一个常见问题是,在同一个上下文中可能存在相同类型的多个Bean定义,使得Spring不确定应该选择哪一个来进行装配。例如,如果有两个不同名称但是实现了同一接口的服务类,并且都标记为了`@Service`,那么就会发生此类情况。为了避免这种情况的发生,可以采用限定符(`@Qualifier`)或者调整Bean的作用域来区分它们[^1]。 #### 3. 初始化顺序问题 Java对象创建过程中存在着特定的初始化顺序:先加载静态成员变量->再加载非静态成员变量->最后才是构造函数执行;而在这些操作之后才会进行带有`@Autowired`标签字段的赋值工作。因此,如果尝试在一个尚未完成整个生命周期的对象内部访问已被标记为`@Autowired`却还未真正初始化完毕的属性,将会得到`null`的结果。对于这个问题,可以通过改变设计模式(比如使用构造器注入而非字段注入),或是利用懒加载特性延迟某些部分的初始化时间点来规避风险。 #### 4. 多线程环境下的特殊处理 在多线程环境下运行的应用程序可能会遭遇额外挑战——尤其是在新启动的工作线程内试图获取来自父级IoC容器中的资源时。默认情况下,子线程并不继承其所在应用程序上下文内的任何已注册过的Beans。为此,一种可行的方法是让当前类实现`ApplicationContextAware`接口并通过它获得全局唯一的`ApplicationContext`引用,进而手动从中取出所需的Bean实例[^4]。 ```java @Component public class MyTaskRunner implements ApplicationContextAware { private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public void run(){ SomeService service = (SomeService)context.getBean(SomeService.class); // Do something with the service... } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值