基于xml注入包含setter、构造、命名空间、集合属性、byName、byType、SPEL、匿名Bean、同类抽象、异类抽象、配置多子配置文件。
package com.athl;
public class Student {
private String name;
private int age;
private School school;
public Student() {
}
public Student(String name, int age, School school) {
super();
this.name = name;
this.age = age;
this.school = school;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSchool(School school) {
this.school = school;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school
+ "]";
}
}
package com.athl;
public class School {
private String name;
private String address;
public School() {
}
public School(String name, String address) {
super();
this.name = name;
this.address = address;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "School [name=" + name + ", address=" + address + "]";
}
}
package com.athl;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Collect {
private School[] schools;
private String[] arrs;
private List list;
private Set set;
private Map map;
private Properties pro;
public Collect() {
}
public Collect(School[] schools, String[] arrs, List list,
Set set, Map map ,Properties pro) {
this.schools = schools;
this.arrs = arrs;
this.list = list;
this.set = set;
this.map = map;
this.pro = pro;
}
public void setSchools(School[] schools) {
this.schools = schools;
}
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public void setList(List list) {
this.list = list;
}
public void setSet(Set set) {
this.set = set;
}
public void setMap(Map map) {
this.map = map;
}
public void setPro(Properties pro) {
this.pro = pro;
}
@Override
public String toString() {
return "Collect [schools=" + Arrays.toString(schools) + ", arrs="
+ Arrays.toString(arrs) + ", list=" + list + ", set=" + set
+ ", map=" + map + ", pro=" + pro + "]";
}
}
package com.athl;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
package com.athl;
public class Worker {
private String name;
private int age;
public Worker() {
}
public Worker(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "worker [name=" + name + ", age=" + age + "]";
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="school" class="com.athl.School">
<constructor-arg name="name" value="哈佛" />
<constructor-arg name="address" value="美国" />
bean>
<bean id="student" class="com.athl.Student" autowire="byName">
<property name="name" value="张三"/>
<property name="age" value="24"/>
bean>
<bean id="student2" class="com.athl.Student">
<property name="name" value="张三"/>
<property name="age" value="24"/>
<property name="school">
<bean class="com.athl.School">
<constructor-arg index="0" value="哈佛2" />
<constructor-arg index="1" value="美国2" />
bean>
property>
bean>
<bean id="pschool" class="com.athl.School" p:name="清华" p:address="中国" />
<bean id="pstudent" class="com.athl.Student" p:name="李四" p:age="22" p:school-ref="pschool" />
<bean id="cschool" class="com.athl.School" c:name="清华" c:address="中国" />
<bean id="cstudent" class="com.athl.Student" c:name="李四" c:age="22" c:school-ref="cschool" />
<bean id="collect" class="com.athl.Collect" >
<property name="schools">
<array>
<ref bean="school"/>
<ref bean="pschool"/>
array>
property>
<property name="arrs" value="张三,李四" />
<property name="list" value="张三,李四" />
<property name="set" value="张三,李四" />
<property name="map">
<map>
<entry key="001" value="张三" />
<entry key="002" value="李四" />
map>
property>
<property name="pro">
<props>
<prop key="001">张三prop>
<prop key="002">李四prop>
props>
property>
bean>
<bean id="person" class="com.athl.Person">
<property name="name" value="nike" />
<property name="age" value="#{T(java.lang.Math).random()*50 }" />
bean>
<bean id="worker" class="com.athl.Worker">
<property name="name" value="#{person.name.toUpperCase() }" />
<property name="age" value="#{person.age }" />
bean>
<bean id="baseWorker" class="com.athl.Worker">
<property name="name" value="码农"/>
bean>
<bean id="worker1" parent="baseWorker">
<property name="age" value="12" />
bean>
<bean id="worker2" parent="baseWorker">
<property name="age" value="34" />
bean>
<bean id="base" abstract="true">
<property name="name" value="小明" />
bean>
<bean id="worker3" class="com.athl.Worker" parent="base">
<property name="age" value="45" />
bean>
<bean id="student3" class="com.athl.Student" autowire="byName" parent="base">
<property name="age" value="24"/>
bean>
beans>
package com.athl.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.athl.Collect;
import com.athl.Person;
import com.athl.Student;
import com.athl.Worker;
public class Mytest {
@Test
public void test(){
/*加载Spring配置文件,创建Spring容器对象,找的是src根目录下配置文件*/
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/*配置多子配置文件(平等关系)*/
/*new ClassPathXmlApplicationContext("spring-*.xml");*/
/*new ClassPathXmlApplicationContext("spring1.xml","spring2.xml");*/
/*数组方式*/
/*
String[] spring={"spring1.xml","spring2.xml"};
new ClassPathXmlApplicationContext(spring);
*/
/*从容器中获取指定Bean对象*/
System.out.println((Student) ac.getBean("student"));
System.out.println((Student) ac.getBean("student2"));
System.out.println((Student) ac.getBean("pstudent"));
System.out.println((Student) ac.getBean("cstudent"));
System.out.println((Collect) ac.getBean("collect"));
System.out.println((Person) ac.getBean("person"));
System.out.println((Worker) ac.getBean("worker"));
System.out.println((Worker) ac.getBean("worker1"));
System.out.println((Worker) ac.getBean("worker2"));
System.out.println((Worker) ac.getBean("worker3"));
System.out.println((Student) ac.getBean("student3"));
}
}