... OrdonnanceController中的字段ordonnanceService需要找不到类型为“ OrdonnanceService”的bean

时间:2020-05-31 11:31:03

标签: java spring autowired

我是Spring Boot的新手,编写文件上传API时出现以下错误:

    > 2020-05-31 12:07:30.713  INFO 22808 --- [           main]
    > o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with
    > port(s): 8080 (http) 2020-05-31 12:07:30.725  INFO 22808 --- [        
    > main] o.apache.catalina.core.StandardService   : Starting service
    > [Tomcat] 2020-05-31 12:07:30.725  INFO 22808 --- [           main]
    > org.apache.catalina.core.StandardEngine  : Starting Servlet engine:
    > [Apache Tomcat/9.0.27] 2020-05-31 12:07:30.908  INFO 22808 --- [      
    > main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring
    > embedded WebApplicationContext 2020-05-31 12:07:30.908  INFO 22808 ---
    > [           main] o.s.web.context.ContextLoader            : Root
    > WebApplicationContext: initialization completed in 3202 ms 2020-05-31
    > 12:07:30.978  WARN 22808 --- [           main]
    > ConfigServletWebServerApplicationContext : Exception encountered
    > during context initialization - cancelling refresh attempt:
    > org.springframework.beans.factory.UnsatisfiedDependencyException:
    > Error creating bean with name 'ordonnanceController': Unsatisfied
    > dependency expressed through field 'ordonnanceService'; nested
    > exception is
    > org.springframework.beans.factory.NoSuchBeanDefinitionException: No
    > qualifying bean of type 'pfa.ordodistance.services.OrdonnanceService'
    > available: expected at least 1 bean which qualifies as autowire
    > candidate. Dependency annotations:
    > {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    > 2020-05-31 12:07:30.982  INFO 22808 --- [           main]
    > o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
    > 2020-05-31 12:07:31.013  INFO 22808 --- [           main]
    > ConditionEvaluationReportLoggingListener : 
    > 
    > Error starting ApplicationContext. To display the conditions report
    > re-run your application with 'debug' enabled. 2020-05-31 12:07:31.153
    > ERROR 22808 --- [           main]
    > o.s.b.d.LoggingFailureAnalysisReporter   : 
    > 
    > *************************** APPLICATION FAILED TO START
    > ***************************
    > 
    > Description:
    > 
    > Field ordonnanceService in
    > pfa.ordodistance.controllers.OrdonnanceController required a bean of
    > type 'pfa.ordodistance.services.OrdonnanceService' 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
    > 'pfa.ordodistance.services.OrdonnanceService' in your configuration.

这是我的班级服务:

@Service
public class OrdonnanceService {

    @Autowired(required = true)
    private OrdonnanceRepository ordonnanceRepository;

    //Save new ordonnance
    public void save(Ordonnance ord) {
        ordonnanceRepository.save(ord);
    }
}

控制器:

@Controller
@ComponentScan("pfa.ordodistance.controllers")
public class OrdonnanceController {

    @Autowired(required = true) 
    private OrdonnanceService ordonnanceService;

    @PostMapping("prescriptionPage")
    public String addNew(Ordonnance ord) {
        ordonnanceService.save(ord);
        return "redirect:/prescriptionPage";
    }
}

存储库:

@Repository
public interface OrdonnanceRepository extends JpaRepository<Ordonnance, Integer> {
}

型号:

@Entity
public class Ordonnance {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    private Date dateO;

    @ManyToOne
    @JoinColumn(name="medecinid", insertable=false, updatable=false)
    private Medecin medecin;
    private Integer medecinid;

    @ManyToOne
    @JoinColumn(name="patientid", insertable=false, updatable=false)
    private Patient patient;
    private Integer patientid;

    public Ordonnance() {
    }

    public Ordonnance(int id, Date dateO, Medecin medecin, Integer medecinid, Patient patient, Integer patientid) {
        this.id = id;
        this.dateO = dateO;
        this.medecin = medecin;
        this.medecinid = medecinid;
        this.patient = patient;
        this.patientid = patientid;
    }
     //getters & setters
}

1 个答案:

答案 0 :(得分:1)

很可能OrdonnanceService无法注册为spring bean,因此无法将OrdonnanceService自动连接到OrdonnanceController并因此导致此错误。

要检查的一件事是确保@ComponentScan涵盖应注册为spring bean的所有必需类。

在您的情况下,将@ComponentScan设置为:

@ComponentScan("pfa.ordodistance.controllers")

它将仅扫描和注册为此组件及其子包而来的spring bean。因此,如果OrdonnanceService位于pfa.ordodistance.service的软件包中,则不会对其进行扫描。

一个简单的尝试就是将其更改为:

@ComponentScan("pfa.ordodistance")

另一方面,如果您遵循docs中建议的最佳实践,该最佳实践将主应用程序类(即用@SpringBootApplication注释的类)放在所有包之上的根包中,则您不需要手动配置@ComponentScan,因为它将扫描此根软件包下的每个类。请参阅上面链接中的示例布局。