Spring实验三 依赖注入(DI)达到解耦

一、依赖注入

简介:Spring开发提倡面向接口编程,配合DI技术实现层与层的解耦。
1.依赖注入(DI):在应用运行期,由外部容器(spring),动态的将依赖对象注入到组件中。
2.激发POJO的潜能
POJO(Plain Old Java object):简单普通的Java类。
Spring用JavaBean来表示应用组件,一个Spring组件可以是任何形式的POJO,
可以说JavaBean是POJO的同义词。
在这里插入图片描述
没有任何地方表明它是一个Spring组件,Spring的非侵入式编程模型意味着这个类在Spring应用中和非Spring应用中都可以发挥同样的作用。
*非侵入式:不强制继承或实现spring的类与接口,这就意味着我们可以在适当的时候安装或卸载spring框架
Spring 赋予POJO魔力的方式之一就是通过DI来装配它们,DI可以帮助应用对象彼此之间保持松散耦合的。

二、DI功能是如何实现的?

1、任何一个有实际意义的应用都会由两个或者更多的类组成,这些类相互之间进行协作来完成特定的业务逻辑。按照传统做法,每个对象负责管理与自己相互协作的对象(即它所依赖的对象)的引用,这将会导致高度耦合和难以测试的代码。
2、通过DI,对象的依赖关系将由系统中负责协调各对象的第三方组件在创建对象的时候进行设定。对象无需自行创建或管理它们的依赖关系。如图3.1依赖关系将自动注入到需要它们的对象当中去。
在这里插入图片描述
如图依赖注入会将所依赖的关系自动交给目标对象,而不是让对象自己去获取依赖

三、实战项目

体会spring DI配合面向接口编程,实现层与层之间的解耦,完成一个字母大小写转换的案例:
思路
1、创建一个接口ChangeLetter
2、定义接口的实现类。
一个实现类:完成大写字母转换为小写字母。
另一个实现类:完成小写字母转换为大写字母。
3、把对象配置到spring容器中。
4、使用对象,实现功能。完成测试。
具体步骤

  1. 定义接口
package com.cxh.inter;
public interface ChangeLetter {
	public String change();
}
  1. 两个实现类
//小写字母转换为大写字母的实现类
package com.cxh.inter;
public class UpperLetter implements ChangeLetter {
	private String str;
	@Override
	public String change() {
		return str.toUpperCase();
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str=str;
	}
}
//大写字母转换为小写字母的实现类
package com.cxh.inter;
public class LowerLetter implements ChangeLetter {
	private String str;
	public String change() {
		return str.toLowerCase();
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str=str;
	}
}
  1. 业务逻辑类
package com.cxh.inter;

public class ChangBll {
	private ChangeLetter chang;
	public void change() {
		System.out.println("ChangBll---change");
		System.out.println(chang.change());	
	}
public void setChang(ChangeLetter chang) {
	this.chang = chang;
}
}
  1. spring.xml
<?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-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 大写变小写 -->
<bean id="change" class="com.cxh.inter.LowerLetter">
    <property name="str"  value="ABRTY"></property>  
</bean> 
<!-- 小写变大写 -->
<!-- <bean id="change" class="com.cxh.inter.UpperLetter">
    <property name="str"  value="abcde"></property>  
</bean> -->
<bean id="changBll" class="com.cxh.inter.ChangBll">
	<property name="chang" ref="change"></property>
</bean>
</beans>
  1. 测试类
package com.cxh.inter;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ChangeLetterTest {
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
		ChangBll changBll=(ChangBll)ac.getBean("changBll");
		changBll.change();
	}
}
  1. 运行结果
    在这里插入图片描述
发布了16 篇原创文章 · 获赞 1 · 访问量 540

猜你喜欢

转载自blog.csdn.net/m0_43455210/article/details/104634212
今日推荐