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

新手上路,请多包涵

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

堆栈跟踪

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$1.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

这是我的 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);
    }
}

这是 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 + "]";
    }
}

这是我的 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">admin@appa.com</prop>
                <prop key="support">support@appa.com"></prop>
                <prop key="hr">hr@appa.com</prop>
            </props>
        </property>
    </bean>
</beans>

原文由 Reeinku 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 526
2 个回答

beans.xml 中,您正在尝试设置字段 adminEmailsCollectionsWithProps 。但是该类没有该字段,它有 emails 字段。

Either fix beans.xml to use emails instead of adminEmails , or fix the source code of CollectionsWithProps be renaming emails to adminEmails (连同 getter 和 setter)

原文由 geoand 发布,翻译遵循 CC BY-SA 3.0 许可协议

属性名称不匹配:

 private Properties emails=null;

理想情况下应该是:

 private Properties adminEmails=null;

getters 和 setters 应该相应地重命名。这将与您在配置文件中提到的相匹配。

原文由 Himanshu Bhardwaj 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题