Spring注解原理学习之@Size and @Pattern

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后一切正常:

你可能感兴趣的:(Java)