XML方式配置Spring
目标
本篇博客的主要目标是使用xml方式配置spring,由spring的IOC容器来实例化对象并设置对应的属性.
使用最简单直观的例子学习spring,了解spring的基本工作原理.
项目结构
关键代码
Person.java
public class Person {
private String name;
private Integer age;
private Car car;
private List<Cat> cats;
private Dog dog;
public Person() {
System.out.println("调用 Person()...");
}
public Person(String name, Integer age) {
System.out.println(String.format(
"调用 Person(String name, Integer age)[name=%s,age=%s]",
name,age));
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("调用 setName()...");
this.name = name;
}
// TODO: 为了避免代码占用过多篇幅,省略了getter,setter和toString()
}
Car.java
public class Car {
private String brand;
private Integer maxSpeed;
private Double price;
private String color;
public Car() {
}
public Car(String brand, Integer maxSpeed, Double price, String color) {
this.brand = brand;
this.maxSpeed = maxSpeed;
this.price = price;
this.color = color;
}
// TODO: 为了避免代码占用过多篇幅,省略了getter,setter和toString()
}
ApplicationContext_01.xml spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Person 相关对象注册 -->
<!-- 调用setter方法 -->
<property name="name" value="Jack2"/>
<property name="age" value="30"/>
<!--引用spring容器内都其他对象-->
<property name="car" ref="car1"/>
</bean>
<!-- 调用无参构造器,调用setter方法-->
<constructor-arg value="Jack4"/>
<constructor-arg value="30"/>
</bean>
pom.xml maven配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>4.3.13.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
TestIOC.java junit测试类
public class TestIOC {
@Test
public void test01(){
System.out.println("开始创建ApplicationContext,初始化IOC容器");
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext_01.xml");
System.out.println("开始根据spring 配置文件中注册的id获取对象");
Person person1= (Person) applicationContext.getBean("person1");
Person person2= (Person) applicationContext.getBean("person2");
Person person3= (Person) applicationContext.getBean("person3");
Person person4= (Person) applicationContext.getBean("person4");
System.out.println("输出获得的对象");
System.out.println(person1);
System.out.println(person2);
System.out.println(person3);
System.out.println(person4);
}
}
测试效果
重点内容
-
ApplicationContext
可以理解为IOC容器,使用new
关键字创建此对象就相当于初始化了IOC容器. - 由于使用XML配置Spring,所以使用
ApplicationContext
的实现类ClassPathXmlApplicationContext
作为具体的实例化类 -
ClassPathXmlApplicationContext
实例化需要classpath下的spring配置文件路径作为参数. - 根据运行效果可以看出,
ApplicationContext
实例化完成后,配置文件ApplicationContext_01.xml
的对象也都调用了构造方法和对应的setter. - 从
ApplicationContext
的对象中获得bean时,使用配置文件中的id
作为参数 ApplicationContext_01.xml
定义了4个Person对象
- person1 最简单bean配置,只需要配置id,class 相当于
Person person1=new Person();
- person2 是在person1的基础上设置属性,
<property name="name" value="Jack2"/>
相当于person2.setName("Jack2");
- person3 与person2作用原理是一致的,只是将
<property name="name" value="Jack2"/>
简写为p:name="Jack3"
- person4 的配置相当于是
Person person4=new Person("Jack4",30);
,每一个<constructor-arg value="Jack4"/>
就是一个构造方法的一个参数 - 所用涉及的赋值的地方都有 value和ref两种,value用于赋值基本类型,ref用于将IOC容器内配置好的对象赋值给相应属性.