【发布时间】:2016-04-18 09:20:49
【问题描述】:
我在 pojo 类中的一些方法得到空值,而其他方法工作正常。 这是我的 POJO 类 Student.java
public class Student {
private int id;
private String name;
private String message1;
private String message2;
private String message3;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void init()
{
System.out.println("Bean is started");
}
public void destroy()
{
System.out.println("Bean is destroyed");
}
public String getMessage1() {
return message1;
}
public void setMessage1(String message1) {
this.message1 = message1;
}
public String getMessage2() {
return message2;
}
public void setMessage2(String message2) {
this.message2 = message2;
}
public String getMessage3() {
return message3;
}
public void setMessage3(String message3) {
this.message3 = message3;
}
}
这是我的xml文件,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-3.0.xsd">
<bean id="student" class="Student">
<property name="id" value="2342"></property>
<property name="name" value="RamaKrishna"></property>
<property name="message1" value="Student--message1"></property>
<property name="message3" value="Student--message3"></property>
</bean>
</beans>
这是我的主要方法逻辑,Logic.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Logic{
public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student)context.getBean("student");
System.out.println(student.getId());
System.out.println(student.getName());
System.out.println(student.getMessage1());
System.out.println(student.getMessage3());
}
}
我正在为第 3 和第 4 个打印语句获取输出空值。 我的输出是:
2342
RamaKrishna
null
null
我应该得到像这样的输出
2342
RamaKrishna
Student--message1
Student--message3
请帮助理解它为什么会这样捐赠。
【问题讨论】:
标签: java xml spring applicationcontext spring-bean