...OrdonnanceController 中的字段 ordonnanceService 需要找不到类型 'OrdonnanceService' 的 bean
Field ordonnanceService in ...OrdonnanceController required a bean of type 'OrdonnanceService' that could not be found
我是 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.
这是我的 class 服务:
@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
}
很可能 OrdonnanceService
无法注册为 spring bean,因此无法将 OrdonnanceService
自动连接到 OrdonnanceController
,从而导致此错误。
要检查的一件事是确保 @ComponentScan
包含所有必需的 class 应该注册为 spring bean 的元素。
在您的情况下,将 @ComponentScan
设置为 :
@ComponentScan("pfa.ordodistance.controllers")
它只会扫描并注册 spring 个为此及其子包而来的 beans。所以如果 OrdonnanceService
在 pfa.ordodistance.service
的包中,则不会被扫描。
一个简单的尝试就是将其更改为:
@ComponentScan("pfa.ordodistance")
另一方面,如果您按照 docs 中建议的最佳实践将主应用程序 class(即用 @SpringBootApplication
注释的 class ) 到所有包之上的根包,你不需要手动配置 @ComponentScan
因为它会扫描这个根包下的每个 classes。请参阅上面的示例布局 link 我的意思是。
我是 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.
这是我的 class 服务:
@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
}
很可能 OrdonnanceService
无法注册为 spring bean,因此无法将 OrdonnanceService
自动连接到 OrdonnanceController
,从而导致此错误。
要检查的一件事是确保 @ComponentScan
包含所有必需的 class 应该注册为 spring bean 的元素。
在您的情况下,将 @ComponentScan
设置为 :
@ComponentScan("pfa.ordodistance.controllers")
它只会扫描并注册 spring 个为此及其子包而来的 beans。所以如果 OrdonnanceService
在 pfa.ordodistance.service
的包中,则不会被扫描。
一个简单的尝试就是将其更改为:
@ComponentScan("pfa.ordodistance")
另一方面,如果您按照 docs 中建议的最佳实践将主应用程序 class(即用 @SpringBootApplication
注释的 class ) 到所有包之上的根包,你不需要手动配置 @ComponentScan
因为它会扫描这个根包下的每个 classes。请参阅上面的示例布局 link 我的意思是。