2

1,Variable 'patientService' might not have been initialized

变量'patientService'可能还没有初始化。

private final PatientService patientService;

image.png

解决:

1.1,只能在构造函数中声明patientService变量

private final PatientService patientService;

    public PatientController(PatientService patientService) {
        this.patientService = patientService;
    }

原因:
1.2,PatientService被我定义了接口,接口是不能被实例化的。不然就会出现报错。
image.png

image.png
1.3总结:
使用final修饰变量,一旦给变量赋值就不能改变;跟angular中的const关键字差不多,一旦被赋值也是不能在修改。
接口不能被初始化,抽象类也不可被实例化的。

2,Could not autowire. No beans of 'PatientService' type found

不能自动连接。没有找到'PatientService'类型的bean
image.png

解决:

在实现类中加入@Service注解即可

@Service
public class PatientServiceImpl implements PatientService

image.png
扩展:
1,bean 是一个由Spring容器管理的对象。Spring容器是一个负责创建、组装和管理对象(即Bean)的容器。
2,标记当前类是一个service类,加上该注解会将当前类自动注入到spring容器中。
3,在构造函数中实例化的PatientService,为什么不把@Service注解放到接口(PatientService)中而是放到实现类型中(PatientServiceImpl);@Service注解是把spring容器中的bean进行实例化的,也就是等同于new操作,只有实现类是可以进行new实例化的,而接口则不能,所以是加在实现类上的。


ngx-validator验证库使用心得:

使用 ngx-validator 替换我们自己写的表单验证库(),真的很方便。
以第三方平台为例,先看我们自己的表单验证库实现的样式:

录屏_选择区域_20231105141655.gif

使用代码:

<form (ngSubmit)="onSubmit()" [formGroup]="formGroup">
  <div class="form-group row">
    <label class="offset-2 col-sm-2 col-form-label text-right"><code>*</code>名称</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" formControlName="clientName" [yzFormValidator]="formGroup.get('clientName')"/>
    </div>
  </div>
...
<div class="form-group row">
    <label class="col-sm-4 col-form-label text-right"><code>*</code>管理用户</label>
    <div class="col-sm-4">
      <app-user-select formControlName="manageUser" [yzFormValidator]="formGroup.get('manageUser')"></app-user-select>
    </div>
  </div>
...
<div class="form-group row ">
    <div class="col-sm-3 offset-9">
      <button type="button" class="btn btn-secondary mr-1" (click)="onClose()">
        <i class="fa far fa-times-circle"></i>关闭</button>
      <button type="submit" class="btn btn-primary" [appButtonValidator]="formGroup"><i class="fa fa-save"></i>确定</button>
    </div>
  </div>
</form>

使用 ngx-validator 表单验证库()实现的效果
录屏_选择区域_20231105141128.gif

<form [formGroup]="formGroup" novalidate [ngxFormValidator]>
  <div class="form-group row">
    <label class="offset-2 col-sm-2 col-form-label text-right"><code>*</code>名称</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" name="clientName" formControlName="clientName"/>
    </div>
  </div>
...
<div class="form-group row">
    <label class="col-sm-4 col-form-label text-right"><code>*</code>管理用户</label>
    <div class="col-sm-4">
      <app-user-select formControlName="manageUser"></app-user-select>
    </div>
  </div>
...

 <button type="submit" class="btn btn-primary" (ngxFormSubmit)="onSubmit()"><i class="fa fa-save"></i>确定</button>
    </div>
  </div>
</form>

可以看到使用表单元素必须设置一个 name,这个 name 会和 ngForm controls 中的对象对应(formControlName);最后提交按钮绑定到 ngxFormSubmit 事件,当按钮被点击后会触发表单验证,验证不通过会根据每个表单元素配置的提示信息反馈错误。

可以看得出真的很方便,但现在对我们生产环境也出了一个问题:

<div class="col-sm-4">
      <app-user-select formControlName="manageUser"></app-user-select>
</div>

app-user-select是一个自定义的组件使用的是ng-zorro中的nz-select,前面说过必须要设置一要name去对应formControlName,但是nz-select是没有name属性的;强加上去运行又可以看到效果
image.png
image.png

进入nz-select的api中又一个nzid参数
image.png

image.png

image.png
这两种处理方式都不行,很容易为后期的维护埋下很大的坑。

参考资料:
https://segmentfault.com/q/1010000042865889
https://zhuanlan.zhihu.com/p/65972116


zZ_jie
462 声望14 粉丝

虚心接受问题,砥砺前行。