我们都知道Spring中有两个重要的知识点,一个是DI注入,一个是AOP切面。那么这两个到底有什么作用呢?
Spring中DI的作用:让相互协作的组件之间保持松耦合(实现面向接口编程)
Spring中AOP的作用:把遍布应用各处的功能分离出来形成可复用的组件
这两句话是我在Spring实战第四版书中看到的。接下来用一个简单的实例来说明一下(把书中的例子小改了一下):
1
2
3
4
5
6
7
8
9
10
11package didemo; /** * 一个简单的任务接口 * @author Logan * @date 2018年8月20日 下午1:28:53 */ public interface Mission { void start(); }
1
2
3
4
5
6
7
8
9
10
11package didemo; /** * 一个简单的英雄接口,只有一个执行任务的方法 * @author Logan * @date 2018年8月20日 下午1:29:28 */ public interface Hero { void executeMission(); }
现在我们想要一个英雄去执行一个任务,如果用传统的方式,是这样的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21package didemo; /** * 不使用Spring的DI注入的传统方式 * @author Logan * @date 2018年8月20日 下午1:31:11 */ public class Wolverine implements Hero { private Mission mission; public Wolverine(Mission mission) { this.mission = new KillMission(); } @Override public void executeMission() { mission.start(); } }
金刚狼Wolverine(我最爱的狼叔)自己创建了杀人任务,所以他和杀人任务紧密的耦合在一起,若还想让他执行其他的任务比如救人的任务就不行了。
那么若使用Spring的DI注入呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package didemo; /** * 一个实现了Mission接口的杀人任务 * @author Logan * @date 2018年8月20日 下午1:33:20 */ public class KillMission implements Mission { @Override public void start() { System.out.println("kill the enemy"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22package didemo; /** * 使用Spring的DI注入 * @author Logan * @date 2018年8月20日 下午1:36:47 */ public class Wolverine implements Hero { private Mission mission; public Wolverine(Mission mission) { this.mission = mission; } @Override public void executeMission() { mission.start(); } }
使用DI注入把任务注入到金刚狼中,和之前金刚狼自己创建任务,这次我们是在构造的时候把任务作为构造器参数传入。这就是构造器注入(DI注入的一种)。
这样做有一个很大的好处就是我们传入的Mission的接口,金刚狼只需要执行传给他的任务就行,他不知道这个任务是什么哪里来的,后期我想让金刚狼执行其他任务的时候只需要实现Mission的接口然后在xml中配置即可。修改很方便,面向接口编程,程序变得很灵活。
接下来再配置一下xml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<?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 id="hero" class="didemo.Wolverine"> <constructor-arg ref="mission" /> </bean> <bean id="mission" class="didemo.KillMission"> </bean> </beans>
配置文件也很简单,只是配置了两个bean而已,接下来测试一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package didemo; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring的DI注入测试类 * @author Logan * @date 2018年8月20日 下午1:42:22 */ // 注意在这个类中,我们只需要执行hero的executeMission方法就可以了,这个类完全不知道是哪个英雄执行了哪个任务 // 只有hero.xml文件知道是哪个英雄执行了什么任务,而且若要修改也很简单,直接修改xml文件即可。 public class HeroMain { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/hero.xml"); Hero hero = context.getBean(Hero.class); hero.executeMission(); context.close(); } }
最后运行输出结果是:kill the enemy。
注意在这个类中,我们只需要执行hero的executeMission方法就可以了,这个类完全不知道是哪个英雄执行了哪个任务,只有hero.xml文件知道是哪个英雄执行了什么任务,而且若要修改也很简单,直接修改xml文件即可。这就是DI注入的好处。
最后
以上就是儒雅猎豹最近收集整理的关于Spring中的DI注入简单实例讲解的全部内容,更多相关Spring中内容请搜索靠谱客的其他文章。
发表评论 取消回复