Created by Wang, Jerry on Jul 27, 2016

起初我直接加入下面的代码,发现不work,perform了validation之后,没有返回我期望的error message:
@Size(min = 10, message = “at least 10 char needed!”)
private String testMin = null;

@Pattern(regexp = “^(\+)?(\d{2,3})?(\s)?(\d{11})$”, message = “invalid phone number”)
private String phone = null;
然后我猜想,是因为我在bean的配置文件里没有将testMin和phone配置成property:

当我加入之后,遇到如下error message:

Jul 27, 2016 11:06:06 AM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘helloWorld’ defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property ‘phone’ of bean class [main.java.com.sap.HelloWorld]: Bean property ‘phone’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘helloWorld’ defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property ‘phone’ of bean class [main.java.com.sap.HelloWorld]: Bean property ‘phone’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at main.java.com.sap.MavenSandbox.main(MavenSandbox.java:17)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property ‘phone’ of bean class [main.java.com.sap.HelloWorld]: Bean property ‘phone’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:230)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:423)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)
… 13 more

这个error message写得很清楚,我这个phone property缺乏对应的setter.
从callstack能看出bean初始化的时候,我们的setter会被框架call到:

加了setter后一切正常:

Spring注解原理学习之@Size and @Pattern相关推荐

  1. 尚硅谷Spring注解开发学习笔记

    文章目录 前言 1.课程安排 1.1.容器 1.2.扩展原理 1.3.Web 2.配置文件开发 2.1.导入Spring-context依赖包 2.2.编写Spring配置文件 2.3.编写Perso ...

  2. Spring注解开发学习笔记

    1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...

  3. Spring MVC原理学习之how is return type handled

    Created by Wang, Jerry, last modified on Aug 22, 2016

  4. Java注解原理学习之@Cacheable debug

    Created by Wang, Jerry, last modified on Dec 30, 2016

  5. spring注解驱动开发-6 Spring AOP实现原理

    Spring AOP实现原理 前言 1.@EnableAspectJAutoProxy注解原理 2.AnnotationAwareAspectJAutoProxyCreator 分析 1.分析前工作, ...

  6. spring注解驱动开发-8 Spring 扩展原理

    Spring 扩展原理 前言 BeanFactoryPostProcessor 测试实例编写 ExtConfig MyBeanFactoryPostProcessor ExtTest 源码分析 Bea ...

  7. Spring 事务原理篇:@EnableTransactionManagement注解底层原理分析技巧,就算你看不懂源码,也要学会这个技巧!

    前言 学习了关于Spring AOP原理以及事务的基础知识后,今天咱们来聊聊Spring在底层是如何操作事务的.如果阅读到此文章,并且对Spring AOP原理不太了解的话,建议先阅读下本人的这篇文章 ...

  8. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考:  Spring 注解学习手札( ...

  9. spring boot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring

    本节主要内容: 1:通过代码演示实现零XML配置spring 2:使用重点注解理解 声明: 本文是<凯哥陪你学系列-框架学习之spring boot框架学习>中spring boot框架学 ...

最新文章

  1. 文件分享微信小程序的设计与开发 Java开发微信小程序 毕业设计
  2. 腾讯回应博鳌AI同传:系谣言,承认出现失误
  3. photoshop中把图片颜色变成透明
  4. 如何列出Git 1.7+中的所有远程分支?
  5. Java的标签--弱化的goto
  6. 按钮添加边框和边框色
  7. springboot(1)使用SpringBoot基础HTTP接口GET|POST|DELETE|PUT请求
  8. rsem比对_RSEM方法比对和表达量计算
  9. 32 CO配置-控制-产品成本控制-成本对象控制-期末结算-定义差异码
  10. 工信部召开地方信息安全工作会议
  11. java中的saturn_Saturn分布式调度平台系列:高屋建瓴之设计原理简析
  12. python基础学习笔记1
  13. 在Android浏览器下字体偏上的问题
  14. android获取截屏图片大小,【Android】android获取长图截屏(显示之外的也要)
  15. 移动硬盘RAW格式,无法识别读取
  16. 【Python数据分析——使用matplotlib制作南丁格尔玫瑰图】
  17. java 仓库类_仓库类型和功能分别是什么?
  18. Core Data的使用
  19. Set、List和数组互相转换
  20. //出生日期转换为多少岁

热门文章

  1. Json.net/Newtonsoft 3.0 新特性JObject/Linq to Json
  2. 【推荐系统(一)】协同过滤之基于领域的方法(UserCF,ItemCF)
  3. 文本挖掘(part5)--文本信息的分布式表示
  4. 文献记录(part55)--基于分布式非负矩阵分解的大规模主题社区挖掘
  5. 第三次学JAVA再学不好就吃翔(part104)--flush和close
  6. 第三次学JAVA再学不好就吃翔(part44)--匿名内部类
  7. 网络编程(part9)--socket套接字编程之TCP套接字
  8. python打包包含所有依赖_解决Python开发过程中依赖库打包问题的方法
  9. 新书发布:时隔一年,我的第二本书终于来了
  10. SAP 电商云 Accelerator 和 Spartacus UI 的工作机制差异