记得导入spring相关jar包
src下bean.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.xsd">
<!-- 第一种,利用带参数的构造器注入,格式如下 -->
<bean name="a1" class="com.edu.bean.Address">
<constructor-arg index="0" type="java.lang.String" value="北京"/>
<constructor-arg index="1" type="java.lang.String" value="清华"/>
</bean>
<!-- 第二种,利用无参构造器注入,格式如下 -->
<bean name="a2" class="com.edu.bean.Address"/>
<!-- 第三种,利用属性的setter方法注入,其格式如下 -->
<bean name="a3" class="com.edu.bean.Address">
<property name="city" value="济南"></property>
<property name="school" value="山大"></property>
</bean>
<!-- 第四种,利用属性的setter方法注入引用属性,其格式如下 -->
<bean name="addr" class="com.edu.bean.Address">
<property name="city" value="山东泰安"></property>
<property name="school" value="山东农业大学"></property>
</bean>
<bean name="ss" class="com.edu.bean.Students">
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="address" ref="addr"></property>
</bean>
</beans>
Address类
package com.edu.bean;
public class Address {
private String city;
private String school;
public Address() {
super();
this.city="泰安";
this.school="农大";
}
public Address(String city, String school) {
super();
this.city = city;
this.school = school;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String toString(){
return "city:"+this.city+"school:"+this.school;
}
}
Students类
package com.edu.bean;
public class Students {
private String name;
private int age;
Address address;
public Students(String name, int age, Address address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public Students() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
测试类
package com.edu.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext act=new ClassPathXmlApplicationContext("bean.xml");
Address address;
address=(Address)act.getBean("a1");
System.out.println("city:"+address.getCity()+"school:"+address.getSchool());
address=(Address)act.getBean("a2");
System.out.println("city:"+address.getCity()+"school:"+address.getSchool());
address=(Address)act.getBean("a3");
System.out.println("city:"+address.getCity()+"school:"+address.getSchool());
Students student;
student=(Students)act.getBean("ss");
//此处需要在Address类里写上toString函数,否则会出现乱码
System.out.println("name:"+student.getName()+"age:"+student.getAge()+"city:"+student.getAddress());
}
}