Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.springframework.beans.NotWritablePropertyException:无效属性';行政电子邮件';豆类_Java_Spring - Fatal编程技术网

Java org.springframework.beans.NotWritablePropertyException:无效属性';行政电子邮件';豆类

Java org.springframework.beans.NotWritablePropertyException:无效属性';行政电子邮件';豆类,java,spring,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

我被下面给出的错误卡住了:

堆栈跟踪

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);
    }
}
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 + "]";
    }
}
下面是集合WithProps.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);
    }
}
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>

admin@appa.com
support@appa.com">
hr@appa.com

属性名称不匹配:

private Properties emails=null;
理想情况下应为:

private Properties adminEmails=null;

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

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


修复
beans.xml
以使用电子邮件而不是
adminEmails
,或者修复
集合的源代码,使用props
电子邮件
重命名为
adminEmails
(以及getter和setter)

我也遇到了同样的问题,只是删除了getter就解决了。因为我的应用程序只需要setter。有时Spring会出现这个错误。

你能发布CollectionsWithProps的代码吗?我现在添加了CollectionsWithProps.javai,我已经修复了beans.xml,并将“adminEmails”改为“emails”“。它正在工作,但当我尝试用setters和getter中的“adminEmails”修复COllectionsWithProps.java中的“emails”时,我再次遇到以前的错误我做错了什么请告诉我…您是否命名了字段
adminEmails
,getter
getAdminEmails
和setter
setAdminEmails
?谢谢,我找到了解决方案。但它想问一个问题,为什么有必要将getter命名为getAdminEmails,将setter命名为setAdminEmails?我现在真的不知道。。。可能与Spring遵守JavaBean规范这一事实有关。您是否可以接受Himanshu Bhardwaj的答案,或者接受Himanshu Bhardwaj的答案,以便未来的读者现在可以知道哪一个是正确的答案,而无需阅读注释?Thanks@PraveenVerma没问题!