0

我正在学习 Spring 并编写一个简单的程序来将属性注入 POJO。下面是主要课程——

public class test {
    public static void main (String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        MySpring sm = (MySpring)context.getBean("myspring");
        System.out.println(sm);
    }

}

POJO 如下——

  public class MySpring {
    public String count;
    void setcount(String val){
        this.count = val;
    }
    String getcount(){
        return count;
    }

}

配置文件如下 -

   <?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-3.0.xsd">
  <bean id="myspring" class="MySpring" >
   <property name="count" value="PowerShell" />
   </bean>
</beans>

但是,当我运行 test.java 类时出现以下错误——

 Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myspring' defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'count' of bean class [MySpring]: Bean property 'count' 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 
.....
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at test.main(test.java:7)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'count' of bean class [MySpring]: Bean property 'count' 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)

我知道这是一个常见错误,但无法找到根本原因,因为一切似乎都很好。任何关于可能是什么问题的指示都将受到高度赞赏。

4

6 回答 6

0

Bean 属性“count”不可写或具有无效的 setter 方法

你需要有一个count属性设置器

于 2012-10-15T06:25:54.213 回答
0

二传手应该是setCount(),不是setcount()

于 2012-10-15T06:27:31.950 回答
0

代码void setcount(String val){应在大写字母中更改为“C”

void setCount(String val)

get/set 后的起始字母应为大写字母。这同样适用于 getter 方法。

于 2012-10-15T06:28:19.527 回答
0

在命名 setter/getter 时,您需要遵循 Javabeans 命名约定。这是 BeanIntrospection 框架的要求。以下应该工作:

void setCount(String val){
  this.count = val;
}

String getCount(){
  return count;
}
于 2012-10-15T06:35:37.467 回答
0

我猜,Spring严格按照naming conventionbean 的属性注入。 Properties总是accessed via method calls on their owning object。对于readable属性,将有一个getter method可读取的值,对于writable属性,将有一个setter method可写入的值。

因此,在您的情况下Spring's IOC container implementation ApplicationContext,尝试实例化bean(MySpring) 和inject您的类的属性,MySpringcount, for injecting purpose container try to finding out the getCount() getter method inside your MySpring class,但您的内部没有这样的方法,class它属于Exception.

修改了你的 bean

class MySpring
{
    private String count;

    /**
     * @return the count
     */
    public String getCount() {
        return count;
    }

    /**
     * @param count the count to set
     */
    public void setCount(String count) {
        this.count = count;
    }   

}
于 2012-10-15T07:16:36.010 回答
0

Spring IoC 容器支持 JavaBeans 规范 ( http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html )中描述的 setter 注入。

它搜索“setCamelVarName()”之类的内容,并将方法名称中“set”之后的第一个字母小写,并按原样使用方法名称的其余部分来推断属性名称。所以,要在你的类中设置“count”属性,你应该声明一个公共方法public void setCount(int count)而不是public void setcount(int count). 无论如何,最后一个也违反了良好的 Java 开发实践。

于 2014-07-02T09:46:47.047 回答