Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException:

StackOverflow https://stackoverflow.com/questions/23653301

  •  22-07-2023
  •  | 
  •  

Question

I was trying to get data from a MySQL database using the Spring utility ResultSetExtractor, but I got the following exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'edao' defined in class path resource [applicationContext2.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbcTemplate' of bean class [org.resultset.EmployeeDao]: Bean property 'jdbcTemplate' 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:1344)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at org.resultset.Test.main(Test.java:11)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbcTemplate' of bean class [org.resultset.EmployeeDao]: Bean property 'jdbcTemplate' 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:1012)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:857)
    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:1341)
    ... 13 more

Employee.java

public class Employee {

    private int id;  
    private String name;  
    private float salary;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getSalary() {
        return salary;
    }
    public void setSalary(float salary) {
        this.salary = salary;
    }
    public Employee(int id, String name, float salary) {
        super();
        this.id = id;
        this.name = name;
        this.salary = salary;
    } 

    public Employee()
    {

    }

}

EmployeeDao.java

public class EmployeeDao {  
private JdbcTemplate template;  

public void setTemplate(JdbcTemplate template) {  
    this.template = template;  
}  

public List<Employee> getAllEmployees(){  
 return template.query("select * from employee",new ResultSetExtractor<List<Employee>>(){  
    @Override  
     public List<Employee> extractData(ResultSet rs) throws SQLException,  
            DataAccessException {  

        List<Employee> list=new ArrayList<Employee>();  
        while(rs.next()){  
        Employee e=new Employee();  
        e.setId(rs.getInt(1));  
        e.setName(rs.getString(2));  
        e.setSalary(rs.getInt(3));  
        list.add(e);  
        }  
        return list;  
        }  
    });  
  }  
}  

Test.java

public class Test {  

public static void main(String[] args) {  
  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext2.xml");  
  EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");  
  List<Employee> list=dao.getAllEmployees();  

  for(Employee e:list)  
      System.out.println(e);  

  }  

}  

and applicationContext2.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="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="com.mysql.jdbc.Driver" />  
<property name="url" value="jdbc:mysql://loclahost:3306/test1" />  
<property name="username" value="root" />  
<property name="password" value="" />  
</bean>  

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  

<bean id="edao" class="org.resultset.EmployeeDao">  
<property name="jdbcTemplate" ref="jdbcTemplate"></property>  
</bean>  

</beans>  

These all are java files I am using. It says the setter's return type doesn't match with the getter's, but I checked it, and it is correct there.

Was it helpful?

Solution

The problem is in

<bean id="edao" class="org.resultset.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

Try changing the name="jdbcTemplate" to name="template". Since you have given name as jdbcTemplate spring will search for a setter method with name setJdbcTemplate() in EmployeeDao class, but the acutal method you have is setTemplate()

OTHER TIPS

Controller extends MethodNameResolver

public final void setMethodNameResolver(
        MethodNameResolver methodNameResolver) {
    this.methodNameResolver = methodNameResolver;
}

public final MethodNameResolver getMethodNameResolver() {
    return this.methodNameResolver;
}

remove spring Annotation like @controller @AutoWired

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top