Java 依赖注入过程中的弹簧误差

Java 依赖注入过程中的弹簧误差,java,spring,spring-mvc,dependency-injection,Java,Spring,Spring Mvc,Dependency Injection,我正在学习Spring并编写一个简单的程序将属性注入POJO。下面是主要课程-- POJO在下面-- 下面是配置文件-- 但是,当我运行test.java类时,我遇到了以下错误-- 线程“main”org.springframework.beans.factory.BeanCreationException中的异常:创建在类路径资源[beans.xml]中定义的名为“myspring”的bean时出错:设置属性值时出错;嵌套异常为org.springframework.beans.NotWr

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

POJO在下面--

下面是配置文件--


但是,当我运行test.java类时,我遇到了以下错误--

线程“main”org.springframework.beans.factory.BeanCreationException中的异常:创建在类路径资源[beans.xml]中定义的名为“myspring”的bean时出错:设置属性值时出错;嵌套异常为org.springframework.beans.NotWritablePropertyException:bean类[MySpring]的属性“count”无效:bean属性“count”不可写或具有无效的setter方法。setter的参数类型与getter的返回类型匹配吗?
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
在
.....
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)
at test.main(test.java:7)
原因:org.springframework.beans.NotWritablePropertyException:bean类[MySpring]的属性“count”无效:bean属性“count”不可写或具有无效的setter方法。setter的参数类型与getter的返回类型匹配吗?
位于org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064)
位于org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:924)
我知道这是一个常见的错误,但我无法找到根本原因,因为一切似乎都很好。非常感谢任何关于可能出现的问题的指针。

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


您需要为
count
属性设置一个setter

设置器应该是
setCount()
,而不是
setCount()
在Caps中,代码
void setCount(String val){
应该更改为“C”


get/set后面的起始字母应该是大写的。getter方法也是如此。

在命名setter/getter时,需要遵循Javabeans命名约定。这是BeanProspection框架的一个要求。 应采取以下措施:

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

String getCount(){
  return count;
}

我猜,
Spring
严格遵循bean属性的
命名约定进行注入。
Properties
总是通过对其所属对象的方法调用来访问
。对于
readable
属性,将有一个
getter
方法来读取那里的值,对于
writeable
属性,将有一个
setter
方法来写入那里的值

因此,在您的例子中,
Spring的IOC容器实现应用程序context
尝试实例化
bean
(MySpring)和
inject
您的
MySpring
类的属性,即
count
,对于
injection-purpose容器,尝试找出getCount()MySpring类中的getter方法,但在属于
异常的
类中没有此类方法

修改你的豆子

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;
    }   

}

SpringIOC容器支持JavaBeans规范()中描述的setter注入

它搜索类似于“setCamelVarName()”的内容,并将方法名称中“set”之后的第一个字母小写,并使用方法名称的其余部分来推断属性名称。 因此,要在类中设置“count”属性,您应该声明一个公共方法
public void setCount(int count)
而不是
public void setCount(int count)

最后一个也反对良好的Java开发实践。

很抱歉我的无知,但是如果您看到我的POJO MySpring,我确实有一个count属性的setter..void setcount(String val){This.count=val;}是否也需要遵守任何特定的约定。是的setter需要是公共的,约定是setcount(),第一个字符应该在CAP中。最好是使用IDE为您创建setter。@user496934:另外,我建议将getter/setter与
public
一起使用,而不是使用默认的访问说明符,以避免在运行时出现访问问题。此外,如果您使用的是像Eclipse这样的IDE,则可以使用菜单选项使Eclipse为您生成getter/setter…@Anshu:我不认为对于依赖注入,我们必须将变量设置为public。我们也可以将其设置为private,但getter-setter应该是public的。请澄清
   <?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>
 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)
void setCount(String val)
void setCount(String val){
  this.count = val;
}

String getCount(){
  return count;
}
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;
    }   

}