目录
1.什么是依赖,什么是注入,注入分几种。哪一种最常用?构造器注入和set注入有什么不同
2.set注入为什么要用到接口?接口有什么好处?什么是多态?多态有什么好处?
1.什么是依赖,什么是注入,注入分几种。哪一种最常用?构造器注入和set注入有什么不同
1).在java中有一种关系叫依赖 什么是依赖呢
举个例子 有两个类--people(人类) --Car(车类)
人想去旅行 要借助车(依赖车) 实现旅行 所以类People 和类Car之间的关系就是依赖关系 即人依赖(拥有/借助)车
2).什么是注入?
在上面People类依赖Car类中 使用的方法叫做 车(Car)类注入到人(People)类中 简称DI 其全名是Dependency injection
3).注入分为两种 一种是构造方法注入 一种是set注入 setr注入最常用
4).构造器注入和set注入实现功能是一样的
只是在使用构造器时 xml文件的代码会比较多 不够清晰明了,没有set一眼明白复杂的依赖关系,代码比较简洁
2.set注入为什么要用到接口?接口有什么好处?什么是多态?多态有什么好处?
因为在注入的实现过程中 会有很多个对象或者说 很多个实现功能的方法 假如说没有接口的话 就需要我们一个一个去写属性和set方法甚至是xml文件
很...麻烦 代码页很繁琐难看,而接口的实现极大的简化并且无缺的实现这个功能,实现很多个对象同时调用一个功能方法。
接口的好处:使用接口可以更加方便 效率 因为在代码中很多地方重复 我们把重复的方法 代码提取出来就很简洁 还有一种情况是处理多变 接口的用途之一就是“多态”。
因为在java中 由于单继承的机制不能实现多个类之间有联系有关系
但是接口完全解决了这个问题 就是可以为两个不相关的类 提供服务 所以 如果两个以上的类 有共同的方法 可以提取成接口 面向接口
多态就是同一个操作作用于不同的对象会有不同的结果。如图:多态的好处是用代表 可以不再改代码
3.set注入在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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!--通过set注入对象-->
<bean id="bus" class="com.spring05.Bus"></bean>
<bean id="human" class="com.spring05.Human">
<!--set注入关键词-->
<property name="bus" ref="bus"></property>
</bean>
</beans>
其中id一般为类名 类名规范第一个字母大写改小写就好了 依赖关系为human依赖(需要/拥有)bus 可以看到id=“human” 包裹name=“bus” 主类 辅类关系是不是就很清晰。
4.编程题:创建一个AdminService的类 负责调用AdminDAO这个接口 dao需要自定义实现类AdminDAOImpl类
AdminDAO 中 增加一个对象() 删除一个对象();
编程描述:AdminService 的类调用AdminDAO这个类 完成增加一个 删除一个对象 注意:DAO必须是接口
主类AdminService :
package com.spring07;
/*主类*/
public class AdminService {
//接口属性
private AdminDAO dao;
//接口的set方法
public void setDao(AdminDAO dao) { this.dao = dao; }
//1.主类AdminService的无参方法
public AdminService(){
System.out.println("AdminService类创建好了");
}
public void zengshangdaima(){
System.out.println("下面是调用增加删除代码");
dao.houtaidaima();
}
}
辅类AdminDAOImpl 实现接口是 implements
package com.spring07;
/*辅类*/
public class AdminDAOImpl implements AdminDAO{
public AdminDAOImpl(){ System.out.println(" AdminDAOImpl类诞生"); }
public void houtaidaima(){
System.out.println("insert into values 表名 (字段名)");
System.out.println("delete from 表名 where id=1");
}
}
接口AdminDAO :
package com.spring07;
/*接口*/
public interface AdminDAO {
void houtaidaima();
}
测试类AdminTest :
package com.spring07;
/*测试类*/
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AdminTest {
public static void main(String[] args) {
//1.获取(加载)xml
ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("spring07.xml");
//2.获取主类
AdminService admin = c.getBean("adminService", AdminService.class);
//3.调用主类方法
admin.zengshangdaima();
}
}
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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="adminDAOImpl" class="com.spring07.AdminDAOImpl"></bean>
<bean id="adminService" class="com.spring07.AdminService">
<property name="dao" ref="adminDAOImpl"></property>
</bean>
</beans>