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

我的应用程序运行但控制台中没有显示任何关于映射的信息。我的应用程序类文件在控制器之上,还添加了 @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")

//@EnableAutoConfiguration

public 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;

Helenr
浏览 95回答 4
4回答

MYYA

您的服务在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以便组件扫描能够拾取它们。

互换的青春

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

绝地无双

取消@Autowired注释。将@Service注释放在实现类而不是接口上,并确保您的实现类可以通过componentScan.此外,作为旁注,Spring 将扫描主类(带@SpringBootApplication注释的类)的所有子包。因此,如果您希望将实现保留在不同的包中,那么拥有com.homzhub.lms一个像根和服务com.homzhub.lms.controller控制器这样的目录结构是个好主意。com.homzhub.lms.servicecom.homzhub.lms.service.impl如果您遵循此结构,则不需要componentScan.

慕工程0101907

您只是在扫描com.homzhub.lms.controller,UserService不在 ComponentScan 下。您需要将服务包添加到 ComponentScan@ComponentScan(basePackages = {"com.homzhub.lms"})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java