Spring框架——採用Java工程實現Hello World!的實現

package com.edu;

public class HelloBean {
	private String name;
	private String course;
	private Double score;
	public HelloBean(String name, String course, Double score) {
		super();
		this.name = name;
		this.course = course;
		this.score = score;
	}
	public HelloBean() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCourse() {
		return course;
	}
	public void setCourse(String course) {
		this.course = course;
	}
	public Double getScore() {
		return score;
	}
	public void setScore(Double score) {
		this.score = score;
	}
	public void show() {
		String s="姓名:  "+this.getName()+"    課程名:"+this.getCourse()+"    成績:"+this.getScore();
           System.out.println(s);	
 	}
	

}
<?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.2.xsd">
<bean name="stu1" class="com.edu.HelloBean">
<property name="name" value="張三"></property>
<property name="course" value="數學"></property>
<property name="score" value="95"></property>
</bean>
<bean name="stu2" class="com.edu.HelloBean">
<property name="name" value="里斯"></property>
<property name="course" value="物理"></property>
<property name="score" value="90"></property>
</bean>
<bean name="stu3" class="com.edu.HelloBean">
<property name="name" value="王五"></property>
<property name="course" value="語文"></property>
<property name="score" value="85"></property>
</bean>

</beans>
package com.edu;

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

public class main {

	public static void main(String[] args) {
		ApplicationContext act=new ClassPathXmlApplicationContext("springjava.xml");
		HelloBean student;
		student=(HelloBean) act.getBean("stu1");
		student.show();
		student=(HelloBean) act.getBean("stu2");
		student.show();
		student=(HelloBean) act.getBean("stu3");
		student.show();
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章