【问题标题】:Consider defining a bean of type 'com.service.AdminService' in your configuration考虑在配置中定义“com.service.AdminService”类型的 bean
【发布时间】:2021-09-22 04:23:59
【问题描述】:

应用程序启动失败


说明:

com.controller.AdminController 中的字段 adminService 需要一个无法找到的“com.service.AdminService”类型的 bean。

注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)

行动:

考虑在您的配置中定义一个“com.service.AdminService”类型的 bean。

【问题讨论】:

  • 你能显示AdminController吗?
  • @VeKe 用 AdminController 更新了问题

标签: java spring spring-boot hibernate spring-mvc


【解决方案1】:

使用这个sn-p的代码就可以了。

@Controller
public class AdminController{

private AdminService adminService;

@Autowired
public AdminController(AdminService adminService){
  this.adminService=adminService;
}
}

并且还要在AdminService接口上方使用@Service注解。

【讨论】:

    【解决方案2】:

    更新到 DoctorOnFingerTipsApplication.java 类中的 @ComponentScan(basePackages = {"com.cybage.*"})

    【讨论】:

    • 试过了,只要我把@ComponentScan(basePackages = {"com.cybage.controller"})改成任何东西,我就会收到这个错误ERROR 26064 o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
    • 您在 DoctorOnFingerTipsApplication.class 中调用了 run 方法两次查看此链接以获取更多参考。stackoverflow.com/questions/48567562/…
    【解决方案3】:

    线

    @ComponentScan(basePackages = {"com.cybage.controller"})
    

    在 DoctorOnFingerTipsApplication.java 中告诉只扫描包 com.cybage.controller 目录中的组件。

    由于您的 AdminService 在 'com.cybage.service' 包中,因此 springboot 不会将其注册为组件。

    因此,请更改 ComponentScan 以扫描您认为您的组件将存在的所有包。如果您打算在 'com.cybage' 包中的目录(例如 /services、/respositories 等)中编写组件,请将 ComponentScan 更新为:

    @ComponentScan(basePackages = {"com.cybage.*"})
    

    其中“*”对应于“com.cybage”包中的所有项目。

    【讨论】:

    • 试过了,只要我把@ComponentScan(basePackages = {"com.cybage.controller"})改成任何东西,我就会收到这个错误ERROR 26064 o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
    • 这完全是 JMXConfiguration 类的另一个错误,与上述问题没有任何联系。您可以在此处查看此问题的答案:stackoverflow.com/questions/41317645/…
    猜你喜欢
    • 2017-03-19
    • 1970-01-01
    • 2018-08-27
    • 2018-06-22
    • 1970-01-01
    • 2020-08-01
    • 2019-02-14
    • 2018-12-21
    • 2019-05-10
    相关资源
    最近更新 更多