Playframework Setter封装

Playframework Setter封装,playframework,playframework-2.0,Playframework,Playframework 2.0,我正在为webapplication使用Play2.0。我开始了解Play的财产模拟:http://www.playframework.org/documentation/1.2.3/model#properties 当我在示例应用程序setter上尝试相同的方法时,并没有调用验证 我在调试模式下重新运行应用程序,并将断点放在Product类的Setter方法上,但它并没有得到执行 下面是代码片段: public class Product { public String name; publ

我正在为webapplication使用Play2.0。我开始了解Play的财产模拟:http://www.playframework.org/documentation/1.2.3/model#properties

当我在示例应用程序setter上尝试相同的方法时,并没有调用验证

我在调试模式下重新运行应用程序,并将断点放在Product类的Setter方法上,但它并没有得到执行

下面是代码片段:

public class Product {

public String name;
public Integer price;

  public void setPrice(Integer price) {
     if (price < 0) {
         throw new IllegalArgumentException("Price can’t be negative!");
     }
     this.price = price;
   }
}    


public class Application extends Controller {

  public static Result index() {
   Product p=new Product();
   p.name="Test";
   p.price=-1; //I am expecting that code will throw IllegalArgumentException but its not

      return ok(index.render(p.name));
  }

}
公共类产品{
公共字符串名称;
公共整数价格;
公共定价(整数价格){
如果(价格<0){
抛出新的IllegalArgumentException(“价格不能为负!”);
}
这个价格=价格;
}
}    
公共类应用程序扩展控制器{
公共静态结果索引(){
产品p=新产品();
p、 name=“测试”;
p、 price=-1;//我希望代码将抛出IllegalArgumentException,但它不是
返回ok(index.render(p.name));
}
}

我在这里遗漏了什么吗?

播放2的行为与播放1的行为不同

它不会重写以下表达式:

anInstance.field = newValue;
因此,您必须直接调用setter来验证参数

资料来源: