【问题标题】:Spring error during dependancy injection依赖注入期间的Spring错误
【发布时间】:2012-10-15 06:24:03
【问题描述】:

我正在学习 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)

我知道这是一个常见错误,但我无法找到根本原因,因为一切似乎都很好。任何关于可能是什么问题的指针都非常感谢。

【问题讨论】:

    标签: java spring spring-mvc dependency-injection


    【解决方案1】:

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

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

    【讨论】:

    • 对不起,我的无知,但是如果你看到我的 POJO MySpring,我确实有一个用于计数属性的设置器..void setcount(String val){ this.count = val; }
    • 是否有任何特定的约定也需要遵守。
    • 是的 setter 需要是 public 并且约定是 setCount(),第一个字符应该在 CAP 中。最好是使用 IDE 为您创建设置器。
    • @user496934 :另外,我建议使用带有public 的getter/setter,而不是使用默认访问说明符以避免运行时出现访问问题。此外,如果您使用的是 Eclipse 之类的 IDE,您可以使用菜单选项让 Eclipse 为您生成 getter/setter...
    • @Anshu :我不认为对于依赖注入,我们必须将变量设置为公共。我们也可以将其保密,但 getter-setter 应该是公开的。请说清楚
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      代码void setcount(String val){在Caps中应改为“C”

      void setCount(String val)
      

      get/set 后的起始字母应为大写字母。 getter 方法也一样。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

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

          因此,在您的情况下,Spring's IOC container implementation ApplicationContext 尝试实例化 bean(MySpring) 和 inject MySpring 类的属性 count,对于 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;
              }   
          
          }
          

          【讨论】:

            【解决方案6】:

            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 开发实践。

            【讨论】:

              猜你喜欢
              • 2016-06-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-13
              • 2018-11-21
              • 2012-12-23
              相关资源
              最近更新 更多