【问题标题】:Getting an error in beans.xml在 beans.xml 中出现错误
【发布时间】:2014-09-26 21:51:34
【问题描述】:

beans.xml 中遇到错误,请参阅此error

我正在 Spring 中制作一个简单的程序,我完全是初学者,我有两个文件。

但是在beans.xml,却在<property name="name" //here is an error.. value="Hello World" />显示错误

上面写着:

Attribute : name 属性的名称,遵循JavaBean命名 约定。

数据类型:字符串

这是我的完整代码:

<?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="helloworld" class="sample1.HelloWorld">
        <property name="message" value="Hello World!!.."/>              
    </bean>

</beans>

正如我之前所说,我有两个文件,其中一个包含HelloWorld.java

package sample1;

public class HelloWorld {
    public String message;

    public void setMessage(){
        this.message=message;
    }

    public void getMessage(){
        System.out.println("Your message: "+message);
    }
}

第二个包含MainProgram.java:

package sample1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainProgram {
    public static void main(String[] args){

        ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld hw=(HelloWorld)context.getBean("helloworld");
        hw.getMessage();
    }
}

我们将不胜感激!

【问题讨论】:

  • 您需要将参数传递给setMessage() -> setMessage(String message)。 Spring 将使用它来设置值。

标签: java xml spring javabeans


【解决方案1】:

根据 Java Beans 规范,它不是有效的 setter 方法。

应该是

public void setMessage(String message){
    this.message=message;
}

【讨论】:

  • 嘿,谢谢它的工作,并想知道一件事.. 我是一个初学者,beans.xml 的实际工作是什么,我的意思是它做什么,你能实际理解我的例子?..
  • 它为您提供对象之间的松散绑定。如果要更改值,则无需更改 java 文件。只需在配置文件中更改即可。
  • 这里有答案可以理解beans.xml stackoverflow.com/questions/14419123/…
  • 在官方文档中阅读更多信息Introduction to the Spring IoC container and beans。关注link了解更多关于spring的信息。
  • @user3218114:谢谢哥们.. :)
猜你喜欢
  • 1970-01-01
  • 2011-10-03
  • 2018-08-11
  • 2011-05-07
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多