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

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

}

猜你喜欢

转载自blog.csdn.net/codertnt/article/details/81150028
今日推荐