gpt4 book ai didi

java - bean 类 [Person] 的无效属性 'messageService ' - Spring Framework

转载 作者:行者123 更新时间:2023-11-30 08:12:52 35 4
gpt4 key购买 nike

我在尝试使用 Spring 框架进行依赖注入(inject)时遇到此错误

Invalid property 'messageService ' of bean class [Person]: Bean property 'messageService ' is not writable or has an invalid setter method. Did you mean 'messageService'?

这是我的类(class)人员:

public class Person {
private MessageService messageService;

public void setMessageService(MessageService messageService){
this.messageService = messageService;
}

public MessageService getMessageService() {
return this.messageService;
}

public void Chao(String Subject, String Message)
{
messageService.sendMessage(Subject,Message);
}

}

这是我的 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="email" class="Email"></bean>
<bean id="Person" class="Person">
<property name="messageService " ref="email"></property>
</bean>
</beans>

最后是主类

public class Main {

public static void main(String args[]) throws Exception{
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

Person test = (Person) context.getBean("Person");
test.Chao("abc","abc");
}
}

MessageService是一个接口(interface),类Email实现了MessageService有人能告诉我我做错了什么吗?非常感谢

最佳答案

name="messageService " 末尾有一个空格。请删除它。

关于java - bean 类 [Person] 的无效属性 'messageService ' - Spring Framework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116394/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com