org.springframework.beans.NotWritablePropertyException:无效的属性' adminEmails'豆类

时间:2014-04-16 06:58:19

标签: java spring

我遇到了下面给出的错误:

堆栈跟踪

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>

3 个答案:

答案 0 :(得分:5)

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

修复beans.xml以使用电子邮件而不是adminEmails,或修复CollectionsWithProps的源代码,将emails重命名为adminEmails(以及获取者)和塞特斯)

答案 1 :(得分:1)

属性名称不匹配:

private Properties emails=null;

理想情况下应该是:

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

答案 2 :(得分:0)

我有同样的问题,只需删除getter即可解决。因为我只需要我的应用程序的setter。有时Spring会给出这个错误。