Exception encountered during context initialization - cancelling refresh attempt

(539 Views)


In this article, we are discussing the Spring org.springframework.beans.factory. NoUniqueBeanDefinitionException - this is a common exception thrown by the BeanFactory when it is unable to set the property of any bean.

Description

Exception thrown on an attempt to set the value of a property that is not writable (typically because there is no setter method)

Complete Exception Stack Trace

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoBean' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'name' of bean class [dao.CoreDao]: Bean property 'name' 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 'daoBean' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'name' of bean class [dao.CoreDao]: Bean property 'name' 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:1642) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1357) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) at test.Test.main(Test.java:12) Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'name' of bean class [dao.CoreDao]: Bean property 'name' 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:243) at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:426) at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278) at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:266) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1638) ... 13 more

Let's see when this Exception occurs with an Example:

Spring Configuration File:

<!-- applicationContext.xml --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean definitions here --> <bean id="daoBean" class="dao.CoreDao"> <property name="name" value="Spring in Action"></property> </bean> </beans>

CoreDao Class:

public class CoreDao { public String name; public void getData() { System.out.println("getting data "); } }

Client Code:

import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.CoreDao; public class Test { public static void main(String[] args) { try(ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");) { CoreDao dao = container.getBean(CoreDao.class); System.out.println("printing name "+dao.name); dao.add(); } } }

Cause Of Exception:

All code is okay but if we look into the applicationContext.xml file , we can easily see that we are setting the value of name attribute of CoreDao class with property tag but we did not have any setter method of 'name' attribute of CoreDao class that is the cause of the exception.

Solution:

Just generate setter and getter method for 'name' attribute of CoreDao class as mentioned below, and all code will work fine

public class CoreDao { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void add() { System.out.println("adding data through hibernate"); } }

Run your project again, application will run without any exception.

If you have any other doubt, feel free to ask us in the comments section, we will try to solve it too.

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search




Search Tags