下载Spring Framework 的 jar
http://repo.spring.io/release/org/springframework/spring/5.0.5.RELEASE/
download spring framework jar
创建一个普通Java Project并引入Spring Framework jar
package com.sheting.springframework.learning.demo01;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Student {
private String name = "sheting";
private int age;
private Course course;
private Teacher teacher;
private List<String> list;
private List<Course> courses;
private Map<String,String> map;
private Properties properties;
private String gender = "男";
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
package com.sheting.springframework.learning.demo01;
public class Course {
private String num;
private String name;
public String getNum() {
return num;
}
public String getName() {
return name;
}
public void setNum(String num) {
this.num = num;
}
public void setName(String name) {
this.name = name;
}
}
<?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-2.0.xsd">
<bean id="student" class="com.sheting.springframework.learning.demo01.Student">
<!-- 注入普通属性 String 和 int,Integer等基本类型和包装类 -->
<property name="name" value="Devin" />
<property name="age" value="26" />
<!-- 引用其他bean -->
<property name="course" ref="course" />
<!-- 注入内部bean, 内部bean仅适用于一次注入,而且不能被其他bean所引用 -->
<property name="teacher">
<bean class="com.sheting.springframework.learning.demo01.Teacher" />
</property>
<!-- 注入List -->
<property name="list">
<list>
<value>aa</value>
<value>bb</value>
<value>cc</value>
</list>
</property>
<property name="courses">
<list>
<ref bean="course" />
</list>
</property>
<!-- 注入map 如果key或value是引用, 要使用key-ref, value-ref -->
<property name="map">
<map>
<entry key="key1" value="value1" />
<entry key="key2" value="value2" />
<entry key="key3" value="value3" />
</map>
</property>
<!-- 装配Properties -->
<property name="properties">
<props>
<prop key="key-1">value-1</prop>
<prop key="key-2">value-2</prop>
</props>
</property>
<!-- 装配空值 -->
<property name="gender"><null/></property>
</bean>
<bean id="course" class="com.sheting.springframework.learning.demo01.Course">
<property name="name" value="化学" />
<property name="num" value="c001" />
</bean>
</beans>
package com.sheting.springframework.learning.demo01;
public class Teacher {
private String name = "jiaoshi";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.sheting.springframework.learning.demo01;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/demo.xml");
Student bean = (Student)context.getBean("student");
System.out.println(bean.getName());
System.out.println(bean.getCourse().getNum());
System.out.println(bean.getTeacher().getName());
System.out.println(bean.getList().get(0));
System.out.println(bean.getCourses().get(0).getName());
System.out.println(bean.getMap().get("key1"));
System.out.println(bean.getProperties().get("key-1"));
System.out.println(bean.getGender());
}
}