我在尝试使用Spring Framework进行依赖注入时遇到此错误


  Bean类[Person]的无效属性'messageService':Bean
  属性“ messageService”不可写或设置无效
  方法。您是说“ messageService”吗?


这是我班的人:

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是接口,类Email implements MessageService
有人可以告诉我我在做什么错吗?非常感谢你

最佳答案

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

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

10-10 12:47