Java org.springframework.beans.NotWritablePropertyException:bean 类的无效属性“adminEmails”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23102193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:42:11  来源:igfitidea点击:

org.springframework.beans.NotWritablePropertyException: Invalid property 'adminEmails' of bean class

javaspring

提问by Praveen Verma

I got stuck on this error given below:

我陷入了下面给出的这个错误:

Stack Trace

堆栈跟踪

Apr 16, 2014 12:21:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'collectionsWithProps' defined in class path resource [beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'adminEmails' of bean class [com.student.spring.impl.CollectionsWithProps]: Bean property 'adminEmails' 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 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at com.student.spring.test.MyTest.main(MyTest.java:26)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'adminEmails' of bean class [com.student.spring.impl.CollectionsWithProps]: Bean property 'adminEmails' 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)
    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:1393)
    ... 8 more

Here is my MyTest.java

这是我的MyTest.java

package com.student.spring.test;

import java.util.Properties;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.student.spring.impl.CollectionsWithProps;

@SuppressWarnings("deprecation")
public class MyTest {
    public static void main(String[] args) {
        Resource resource = new ClassPathResource("beans.xml");
        BeanFactory beanFactory = new XmlBeanFactory(resource);

        CollectionsWithProps cWP = (CollectionsWithProps) beanFactory
                .getBean("collectionsWithProps");
        System.out.println(cWP);
    }
}

Here is CollectionsWithProps.java

这是CollectionsWithProps.java

package com.student.spring.impl;

import java.util.Properties;

public class CollectionsWithProps {
    private Properties emails=null;
    public Properties getEmails() {
        return emails;
    }
    public void setEmails(Properties emails) {
        this.emails = emails;
    }
    public String toString(){
        return "College [Props=" + emails + "]";
    }
}

Here is my beans.xml

这是我的beans.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="collectionsWithProps" class="com.student.spring.impl.CollectionsWithProps">
        <property name="adminEmails">
            <props>
                <prop key="admin">[email protected]</prop>
                <prop key="support">[email protected]"></prop>
                <prop key="hr">[email protected]</prop>
            </props>
        </property>
    </bean>
</beans>

采纳答案by geoand

In beans.xmlyou are trying to set the field adminEmailsof CollectionsWithProps. However the class doesn't have that field, it has the emailsfield.

beans.xml您要设置字段adminEmailsCollectionsWithProps。但是该类没有该字段,它具有该emails字段。

Either fix beans.xmlto use emails instead of adminEmails, or fix the source code of CollectionsWithPropsbe renaming emailsto adminEmails(along with the getters and setters)

要么修复beans.xml使用电子邮件而不是adminEmails,要么修复CollectionsWithProps重命名emails为的源代码adminEmails(以及getter和setter)

回答by Himanshu Bhardwaj

There is property name mismatch:

属性名称不匹配:

private Properties emails=null;

should ideally be:

理想情况下应该是:

private Properties adminEmails=null;

getters and setters should be renamed accordingly. This will match with what you have mentioned in the configuration files.

getter 和 setter 应该相应地重命名。这将与您在配置文件中提到的内容相匹配。

回答by Vinodini Thambudurairaj

I had the same issue and it got resolved by just removing getter. As I required only setter for my application. Sometimes Spring gives this error.

我遇到了同样的问题,只需删除 getter 即可解决。因为我的应用程序只需要设置器。有时 Spring 会出现此错误。