和bean属性约束之级联约束一样,方法约束也支持级联约束。直接看例子:
- Car定义
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
public Car(String manufacturer, String licencePlate) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
}
- Garage定义
public class Garage {
@NotNull
private String name;
// 返回值约束
@Valid
public Garage(String name) {
this.name = name;
}
// 参数约束
public boolean checkCar(@Valid @NotNull Car car) {
return false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在上面的例子中,当验证checkCar方法的参数的时候,定义在car对象上的约束也会被计算。同样的,当验证Garage构造方法的返回值的时候,Garage对象的name属性上的@NotNull约束也会被计算。
总之,方法约束和普通属性约束类似,也支持级联约束,具体细节可以参考普通属性的级联约束