spring boot 中@Autowired注解无法自动注入的错误

编程知识 更新时间:2023-04-05 04:46:42

spring boot 中@Autowired注解无法自动注入的错误

在使用springboot main方法启动项目时遇到[@Autowdired]注解无法注入,网上查了很多资料也是一位网上的大神的一句话理通了思路:

异常信息:

异常信息:
Action:Consider defining a bean of type 'com.boot.app.service.bootService' in your configuration.
controller层:

@RestController

    @Autowired
    private BootService bootService;

    @RequestMapping(value="/query",method=RequestMethod.POST)
    public String queryByStatus() {
        return "query";
    }
}

发现注解均正常:

SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!“Application类”是指SpringBoot项目入口类。这个类的位置很关键:如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描!即, 把Application类放到dao、service所在包的上级,com.boot.Application知道这一点非常关键

Application类:

@SpringBootApplication
public class Application  extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);     
    }

更多推荐

spring boot 中@Autowired注解无法自动注入的错误

本文发布于:2023-04-05 04:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/95f37370b5f1522f743f1c627f5f82b8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注解   错误   spring   boot   Autowired

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 44848文章数
  • 14阅读数
  • 0评论数