# 3Spring IoC初体验
# Spring IoC初体验-示例代码
- 妈妈在早晨后给三个孩子分发餐后水果
- 盘子里有三个苹果:红富士、青苹果、金帅
- 孩子们口味不同:莉莉喜欢甜的、安妮喜欢酸的、露娜喜欢软软的
孩子如何得到喜欢的苹果呢?
# 原始代码实现如下功能
pom.xml
<?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>
<groupId>com.torey.spring</groupId>
<artifactId>s01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
</dependencies>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.torey.spring.ioc.entity;
import lombok.Data;
@Data
public class Apple {
private String title;
private String color;
private String origin;
public Apple() {
}
public Apple(String title, String color, String origin) {
this.title = title;
this.color = color;
this.origin = origin;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.torey.spring.ioc.entity;
import lombok.Data;
@Data
public class Child {
private String name;
private Apple apple;
public Child() {
}
public Child(String name, Apple apple) {
this.name = name;
this.apple = apple;
}
public void eat(){
System.out.println(name + "吃到了" + apple.getOrigin() + "种植的" + apple.getTitle());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.torey.spring.ioc;
import com.torey.spring.ioc.entity.Apple;
import com.torey.spring.ioc.entity.Child;
public class Application {
public static void main(String[] args) {
Apple apple1 = new Apple("红富士", "红色", "欧洲");
Apple apple2= new Apple("青苹果", "青色", "中亚");
Apple apple3 = new Apple("金帅", "金色", "中国");
Child lili = new Child("莉莉", apple1);
Child andi = new Child("安迪", apple2);
Child luna = new Child("露娜", apple3);
lili.eat();
andi.eat();
luna.eat();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
原始代码的缺点:
- 红富士、青苹果等这些文字是写死在程序里的,一旦新增一个新的品种,就需要修改代码,重新发布
- 通过new关键字关联孩子与苹果的关系也是写死的,一旦某一孩子想换另外一种口味,也需要修改代码,重新发布
# spring方式
<dependency>
<!--spring-context:spring上下文,也是spring的最小应用范围-->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
- spring-core:整个spring最核心的代码,所有spring最底层的代码都是从这里开始写的
- spring-beans:是spring对bean(也就是java对象)的管理模块,实例化、对象关联都需要通过这个模块
- spring-expression:是spring内置的表达式模块,spring在使用过程中有一个属于自己的成体系的表达式语言,在实际开发的时候这些表达式就会通过这个依赖来进行解析和处理
- spring-jcl:jcl是spring与日志交互的模块,我们在运行过程中所产生的的一系列底层的日志都是由这个日志模块来处理的
- spring-aop:则是对应了面向切面编程的模块,在引入context之后,AOP模块也会自动引入
- spring-context:spring的上下文,通过spring-context中的applictionContext对象可以让我们通过代码来对spring IoC容器进行创建
# 在resources目录新建applicationContext.xml
applicationContext.xml的文件内存可以从官网复制 (opens new window)
<?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">
<!--在IoC容器启动时,自动由Spring实例化Apple对象,取名sweetApple放入到容器中-->
<bean id="sweetApple" class="com.torey.spring.ioc.entity.Apple">
<property name="color" value="红色"/>
<property name="origin" value="欧洲"/>
<property name="title" value="红富士"/>
</bean>
<bean id="sourApple" class="com.torey.spring.ioc.entity.Apple">
<property name="color" value="青色"/>
<property name="origin" value="中亚"/>
<property name="title" value="青苹果"/>
</bean>
<bean id="softApple" class="com.torey.spring.ioc.entity.Apple">
<property name="color" value="金色"/>
<property name="origin" value="中国"/>
<property name="title" value="金帅"/>
</bean>
<bean id="lili" class="com.torey.spring.ioc.entity.Child">
<property name="name" value="莉莉"/>
<property name="apple" ref="softApple"/>
</bean>
<bean id="andi" class="com.torey.spring.ioc.entity.Child">
<property name="name" value="安迪"/>
<property name="apple" ref="sweetApple"/>
</bean>
<bean id="luna" class="com.torey.spring.ioc.entity.Child">
<property name="name" value="露娜"/>
<property name="apple" ref="sourApple"/>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 新建java文件
package com.torey.spring.ioc;
import com.torey.spring.ioc.entity.Apple;
import com.torey.spring.ioc.entity.Child;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
//classpath: 代表当前目录下
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Apple sweetApple = (Apple)context.getBean("sweetApple");
Apple sourApple = context.getBean("sourApple",Apple.class);
System.out.println(sweetApple.toString());
System.out.println(sourApple.toString());
Child lili = context.getBean("lili", Child.class);
Child andi = context.getBean("andi", Child.class);
Child luna = context.getBean("luna", Child.class);
lili.eat();
andi.eat();
luna.eat();
System.out.println("是否是同一个对象");
System.out.println(andi.getApple().hashCode());
System.out.println(sweetApple.hashCode());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27