java – 设置属性值时出错;嵌套异常是org.springframework.beans.NotWritablePropertyException:

前端之家收集整理的这篇文章主要介绍了java – 设置属性值时出错;嵌套异常是org.springframework.beans.NotWritablePropertyException:前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图使用Spring实用程序ResultSetExtractor从MySQL数据库获取数据,但我得到以下异常:

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.beanfactory.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 ListsqlException,DataAccessException {  

        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

和applicationContext2.xml

MysqL.jdbc.Driver" />  
MysqL://loclahost:3306/test1" />  

这些都是我正在使用的java文件.它说setter的返回类型与getter不匹配,但我检查了它,并且它在那里是正确的.

最佳答案
问题在于

< bean id =“edao”class =“org.resultset.EmployeeDao”>
     < property name =“jdbcTemplate”ref =“jdbcTemplate”>< / property>
< /豆腐>

尝试将name =“jdbcTemplate”更改为name =“template”.由于你已经将名称命名为jdbcTemplate,spring将在EmployeeDao类中搜索名为setJdbcTemplate()的setter方法,但是你拥有的acutal方法是setTemplate()

原文链接:https://www.f2er.com/spring/431817.html

猜你在找的Spring相关文章