为了账号安全,请及时绑定邮箱和手机立即绑定

其余控制器未在 spring boot 中映射

其余控制器未在 spring boot 中映射

Helenr 2023-03-23 15:26:51
我的应用程序运行但控制台中没有显示任何关于映射的信息。我的应用程序类文件在控制器之上,还添加了 @ComponentScan(basePackages : "com.org.name.controller") 来扫描控制器。仍然没有映射显示在控制台中。我在控制器类中注释掉了@Autowired,因为我收到以下错误:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.2019-07-12 11:05:16.014 ERROR 14104 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************APPLICATION FAILED TO START***************************Description:Field userService in com.homzhub.lms.controller.UserController required a bean of type 'com.homzhub.lms.service.UserService' that could not be found.The injection point has the following annotations:    - @org.springframework.beans.factory.annotation.Autowired(required=true)Action:Consider defining a bean of type 'com.homzhub.lms.service.UserService' in your configuration.主要类:package com.homzhub.lms;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;@SpringBootApplication@ComponentScan(basePackages = {"com.homzhub.lms.controller"})//@EnableJpaRepositories("repository")//@EnableAutoConfigurationpublic class LmsApplication{    public static void main(String[] args){        SpringApplication.run(LmsApplication.class, args);    }}预约控制器:package com.homzhub.lms.controller;import java.security.Principal;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.homzhub.lms.entity.Appointment;import com.homzhub.lms.entity.User;import com.homzhub.lms.service.AppointmentService;import com.homzhub.lms.service.UserService;
查看完整描述

4 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

您的服务在com.homzhub.lms.service包下,因此您也必须将此包添加到其中@ComponentScan,因此 Spring 也会扫描此包并选择标有构造型的类:


@SpringBootApplication

@ComponentScan(basePackages = {"com.homzhub.lms.controller, "com.homzhub.lms.service"})

public class LmsApplication{

    public static void main(String[] args){

        SpringApplication.run(LmsApplication.class, args);

    }

}

但是,我可以看到您的带有注释的类已经在所有包含您的组件的包之上,因此您可以完全@SpringBootApplication摆脱注释。@ComponentScan所以它会默认扫描嵌套包。


还要记住使用 Spring 构造型注释来注释您的服务类,@Service以便组件扫描能够拾取它们。


查看完整回答
反对 回复 2023-03-23
?
互换的青春

TA贡献1797条经验 获得超6个赞

如果还没有,您需要将其定义UserService为组件,或更恰当地定义为服务。如果它已经是你必须映射它,考虑到 Spring 应该自己做这件事,这有点奇怪。



查看完整回答
反对 回复 2023-03-23
?
绝地无双

TA贡献1946条经验 获得超4个赞

取消@Autowired注释。将@Service注释放在实现类而不是接口上,并确保您的实现类可以通过componentScan.

此外,作为旁注,Spring 将扫描主类(带@SpringBootApplication注释的类)的所有子包。因此,如果您希望将实现保留在不同的包中,那么拥有com.homzhub.lms一个像根和服务com.homzhub.lms.controller控制器这样的目录结构是个好主意。com.homzhub.lms.servicecom.homzhub.lms.service.impl

如果您遵循此结构,则不需要componentScan.


查看完整回答
反对 回复 2023-03-23
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

您只是在扫描com.homzhub.lms.controllerUserService不在 ComponentScan 下。您需要将服务包添加到 ComponentScan

@ComponentScan(basePackages = {"com.homzhub.lms"})


查看完整回答
反对 回复 2023-03-23
  • 4 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信