第一个 Spring IoC 应用
在这一章节,我将向大家展示如何通过 Spring IoC 进行对象管理,而无需在程序中一个个 new 对象
Maven 加入 Spring Framework依赖
在工程的 pom.xml 中加入 springframework 依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
</dependencies>
定义新建两个实体类
定义两个实体类 Apple 和 Children, 其相互关系是 小孩吃苹果
package indi.chester.spring.ioc.entity;
public class Apple {
private String title;
private String color;
private String origin;
public Apple(String title, String color, String origin) {
this.title = title;
this.color = color;
this.origin = origin;
}
public Apple() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
}
package indi.chester.spring.ioc.entity;
public class Child {
private String name;
private Apple apple;
public Child(String name, Apple apple) {
this.name = name;
this.apple = apple;
}
public Child() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Apple getApple() {
return apple;
}
public void setApple(Apple apple) {
this.apple = apple;
}
public void eat(){
System.out.println(this.getName()+" 吃 " + this.getApple().getTitle());
}
}
配置 applicationContext.xml 文件
在src/main/resources 目录下创建 applicationContext.xml 文件,这个文件是用来配置我们的 Spring IoC 容器的。
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sweetApple" class="indi.chester.spring.ioc.entity.Apple">
<property name="title" value="红富士"/>
<property name="origin" value="欧洲"/>
<property name="color" value="红色"/>
</bean>
<bean id="sourApple" class="indi.chester.spring.ioc.entity.Apple">
<property name="title" value="青苹果"/>
<property name="origin" value="中亚"/>
<property name="color" value="绿色"/>
</bean>
<bean id="lily" class="indi.chester.spring.ioc.entity.Child">
<property name="name" value="Lily"/>
<property name="apple" ref="sweetApple"/>
</bean>
<bean id="tom" class="indi.chester.spring.ioc.entity.Child">
<property name="name" value="Tom"/>
<property name="apple" ref="sourApple"/>
</bean>
</beans>
在这个 xml 文件开头的 beans 标签 内的 xmlns 属性是固定写法,直接照抄即可。
beans 标签可以理解为对象的集合,beans 标签下面的 bean 标签 对应一个个对象。在bean 标签下,定义了 对象的id(id必须唯一), 类的完整路径和类的成员属性。
在Spring IoC 会通过构造方法来创建对象。 打个比方,Spring IoC就好比一个篮子,将想要的对象先放进去,后面随时可以拿。
简单测试
package indi.chester.spring.ioc;
import indi.chester.spring.ioc.entity.Apple;
import indi.chester.spring.ioc.entity.Child;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
//创建 Spring IoC 容器, 并配置文件在容器中实例化对象
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Apple apple1=context.getBean("sweetApple",Apple.class);
System.out.println(apple1.getTitle());
Child lily = context.getBean("lili",Child.class);
lily.eat();
}
}
}j
我们通过创建 ApplicationContext 对象,完成对 Spring IoC 容器的实例化,随后直接通过 context.getBean 方法即可直接从 Spring IoC容器中取出对象。
如果想要修改对象,比如增加,删除对象,我们直接在 applicationContext.xml 文件中修改即可,尽可能降低了 Java 文件的修改。而且对象的信息在 xml 文件中一眼可以看出来,而无需到 java 文件中去寻找,实现了代码的解耦。
Last updated