Thursday, March 26, 2015

Exceptions in spring configuration

This is to give you some details about the exceptions and how will they come. I have refered the code I explained in the
http://cgenit.blogspot.com/2015/03/spring-basic-collection-list.html  to generate the exceptions.

Here we have used a list points inside the Triangle class and we are injecting the beans for the Triangle.
We have circle bean definition , another bean we use.

So to refresh the xml configuration, it will look like below


spring.xml
--------------------------------------------------------------------------------------------------------------------
<bean name="triangle" class="org.aop.model.Triangle">
<property name="name" value="Traingle name"></property>
<property name="points">
<list>
<ref bean="zeroPoint" />
<ref bean="pointTwo" />
<ref bean="pointThree" />
</list>
</property>
</bean>

<bean name="zeroPoint" class="org.aop.model.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<bean name="pointTwo" class="org.aop.model.Point">
<property name="x" value="20"></property>
<property name="y" value="0"></property>
</bean>
<bean name="pointThree" class="org.aop.model.Point">
<property name="x" value="0"></property>
<property name="y" value="20"></property>
</bean>

<bean name="circle" class="org.aop.model.Circle">
<property name="name" value="Circle name"></property>
</bean>
---------------------------------------------------------------------------------------------------------------

 Error No 1: Let's add a circle to the points list. We have defined list as Points generic list inside the Triangle object.

-------------------------------------------------------------------------------------------

package org.aop.model;

import java.util.List;

public class Triangle {
private String name;
private List points;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getPoints() {
return points;
}
public void setPoints(List points) {
this.points = points;
}
@Override
public String toString() {
return "Triangle [name=" + name + ", points=" + points + "]";
}

}

------------------------------------------------------------------------------

Now this will give us conversion error and tell us bean creation does not know any strategy to convert the circle bean to Point. I have highlighted the text line where we can identify the error.


error code , adding circle bean highlighted.

----------------------------------------------------

<bean name="triangle" class="org.aop.model.Triangle">
<property name="name" value="Traingle name"></property>
<property name="points">
<list>
<ref bean="zeroPoint" />
<ref bean="pointTwo" />
<ref bean="pointThree" />
<ref bean="circle" />
</list>
</property>
</bean>

--------------------------------------------


Below is the exception.
---------------------------------------------------------------------------------------------------

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.util.List' for property 'points'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.aop.model.Circle$$EnhancerBySpringCGLIB$$bb74d74b] to required type [org.aop.model.Point] for property 'points[3]': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at org.aop.main.AppMain.main(AppMain.java:10)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.util.List' for property 'points'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.aop.model.Circle$$EnhancerBySpringCGLIB$$bb74d74b] to required type [org.aop.model.Point] for property 'points[3]': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:463)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:494)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:488)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1463)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 11 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.aop.model.Circle$$EnhancerBySpringCGLIB$$bb74d74b] to required type [org.aop.model.Point] for property 'points[3]': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:562)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:202)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
... 17 more


------------------------------------------------------------------------------------------------------------------------


2. Error 2: let's try to add the Triangle class to Point bean definition.
Point class has two properties with name "x" and "y" and we have defined the values to be inject in bean creation in our spring.xml configuration. 
Now we have mistakenly given Triangle class to place where we need to give Point class.
As Triangle does not have setter for the defined property values it will give error when setting those properties when creating bean from the definition given.

error code inside spring.xml


------------------------------------------------------------------------------------------------------------------------
<bean name="zeroPoint" class="org.aop.model.Triangle">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
------------------------------------------------------------------------------------------------------------------------

Below is the exception
------------------------------------------------------------------------------------------------------------------------
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Cannot resolve reference to bean 'zeroPoint' while setting bean property 'points' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'zeroPoint' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'x' of bean class [org.aop.model.Triangle]: Bean property 'x' 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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:326)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:350)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1417)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at org.aop.main.AppMain.main(AppMain.java:10)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'zeroPoint' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'x' of bean class [org.aop.model.Triangle]: Bean property 'x' 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:1453)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:320)
... 17 more
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'x' of bean class [org.aop.model.Triangle]: Bean property 'x' 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.setPropertyValue(BeanWrapperImpl.java:1043)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:903)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1450)
... 25 more

------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment