javascript
Spring注解原理学习之@Size and @Pattern
Created by Wang, Jerry on Jul 27, 2016
起初我直接加入下面的代碼,發(fā)現(xiàn)不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:
當(dāng)我加入之后,遇到如下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缺乏對應(yīng)的setter.
從callstack能看出bean初始化的時候,我們的setter會被框架call到:
加了setter后一切正常:
總結(jié)
以上是生活随笔為你收集整理的Spring注解原理学习之@Size and @Pattern的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring学习笔记之Design of
- 下一篇: 王者荣耀出征活动在哪里