【发布时间】: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 将使用它来设置值。