天天看点

springboot @Autowired自动注入repository失败解决方法

我的报错结果如下:Field authorRepository in com.example.sptest.controller.Controller required a bean named 'entityManagerFactory' that could not be found.

解决方法:

  • 确定你创建的repository位置在与启动类xxxApplication平级的包里或子包里
  • 查看启动类XXXApplication,有没有像我这样在@SpringBootApplication里添加
    exclude = {DataSourceAutoConfiguration.class}
               

      有的话,删掉应该就能成功自动注入了。

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SptestApplication {

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

继续阅读