'org.springframework.beans.NotWritablePropertyException: Invalid property 'adminEmails' of bean class

I got stuck on this error given below:

Stack Trace

Apr 16, 2014 12:21:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'collectionsWithProps' defined in class path resource [beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'adminEmails' of bean class [com.student.spring.impl.CollectionsWithProps]: Bean property 'adminEmails' 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:1396)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at com.student.spring.test.MyTest.main(MyTest.java:26)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'adminEmails' of bean class [com.student.spring.impl.CollectionsWithProps]: Bean property 'adminEmails' 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:1064)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:924)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
    ... 8 more

Here is my MyTest.java

package com.student.spring.test;

import java.util.Properties;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.student.spring.impl.CollectionsWithProps;

@SuppressWarnings("deprecation")
public class MyTest {
    public static void main(String[] args) {
        Resource resource = new ClassPathResource("beans.xml");
        BeanFactory beanFactory = new XmlBeanFactory(resource);

        CollectionsWithProps cWP = (CollectionsWithProps) beanFactory
                .getBean("collectionsWithProps");
        System.out.println(cWP);
    }
}

Here is CollectionsWithProps.java

package com.student.spring.impl;

import java.util.Properties;

public class CollectionsWithProps {
    private Properties emails=null;
    public Properties getEmails() {
        return emails;
    }
    public void setEmails(Properties emails) {
        this.emails = emails;
    }
    public String toString(){
        return "College [Props=" + emails + "]";
    }
}

Here is my beans.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" xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="collectionsWithProps" class="com.student.spring.impl.CollectionsWithProps">
        <property name="adminEmails">
            <props>
                <prop key="admin">[email protected]</prop>
                <prop key="support">[email protected]"></prop>
                <prop key="hr">[email protected]</prop>
            </props>
        </property>
    </bean>
</beans>


Solution 1:[1]

In beans.xml you are trying to set the field adminEmails of CollectionsWithProps. However the class doesn't have that field, it has the emails field.

Either fix beans.xml to use emails instead of adminEmails, or fix the source code of CollectionsWithProps be renaming emails to adminEmails (along with the getters and setters)

Solution 2:[2]

There is property name mismatch:

private Properties emails=null;

should ideally be:

private Properties adminEmails=null;

getters and setters should be renamed accordingly. This will match with what you have mentioned in the configuration files.

Solution 3:[3]

I had the same issue and it got resolved by just removing getter. As I required only setter for my application. Sometimes Spring gives this error.

Solution 4:[4]

Check for name value in .xml and Variables spelling And case should be same.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 geoand
Solution 2 Himanshu Bhardwaj
Solution 3 Vinodini Thambudurairaj
Solution 4 Sukhdev Sharma