您的位置:首页 > 编程语言 > Java开发

那些年、一起追过的Spring--(2)----IOC、两种依赖注入方式

2017-09-07 10:30 756 查看

IOC

提纲–

接口及面向接口编程

什么是IOC

Spring 的Bean配置

Bean的初始化

Spring的常用注入方式

接口

用于沟通的中介物的抽象化

实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式。

对应Java接口即声明,声明了那些方法是对外公开提供的

在Java8中,接口可以拥有方法体。

面向接口编程

结构设计中,分清层次及调用关系,每层只向外(上层)提供一组功能接口,各层间仅依赖接口而非实现类

接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要

“面向接口编程”中的“接口”是用于影藏具体实现和实现多态性的组件

什么是IOC

IOC:(Inversion of Control)控制反转是Spring的核心,贯穿始终。所谓IoC,对于spring框架来说,就是有Spring来负责控制对象的生命周期和对象键的关系。

–传统开发模式:对象之间相互依赖

– IoC开发模式:IoC容器安排对象之间的依赖。

DI:(Dependency Injection),所谓的依赖注入,就是由IOC容器在运行期间,动态的将某种依赖关系注入到对象之中。

目的:

创建对象并且组成对象之间的关系

IoC的好处

IoC在编程过程中不会对业务对象构成很强的侵入性,使用IoC之后,对象具有更好的可实行性,可重用性和可扩展性;

降低组件之间的耦合对

提高开发效率和产品质量

统一标准,提高模块的复用性

模块具有热插拔特性

IoC通俗的理解如下:

IoC控制反转:说的是创建对象实例的控制权从代码控制剥离到IoC容器控制,实际就是你在xml文件控制,侧重于原理

引用地址:http://www.jikexueyuan.com/course/665_2.html?ss=1

Bean容器初始化

文件:

– FileSystemXlmApplicationContext

Classpath:

– ClassPathXmlApplicationContext

Web应用:

两种依赖注入的方式:

设值注入

构造注入

通过setter方式注入(设值注入):

package com.gxa.spring.day01;

public class ItemDaoImpl {
public ItemDaoImpl() {
System.out.println("ItemDaoImpl的构造方法!");
}
}


package com.gxa.spring.day01;

public class PetDaoImpl {
public PetDaoImpl() {
System.out.println("PetDaoImpl的构造方法");
}
}


通过在Service中设置itemDao,和petDao属性的setter方法:

package com.gxa.spring.day01;

public class PetServiceImpl {
private ItemDaoImpl itemDao;
private PetDaoImpl petDao;
public void setItemDao(ItemDaoImpl itemDao) {
this.itemDao = itemDao;
}
public void setPetDao(PetDaoImpl petDao) {
this.petDao = petDao;
}
}


<?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="petService" class="com.gxa.spring.day01.PetServiceImpl">
<property name="itemDao" ref="itemDao"></property>
<property name="petDao" ref="petDao"></property>
</bean>
<bean id="petDao" class="com.gxa.spring.day01.PetDaoImpl"></bean>
<bean id="itemDao" class="com.gxa.spring.day01.ItemDaoImpl"></bean>
</beans>


测试:

public class test02 {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(
"spring.xml");
PetServiceImpl petService = (PetServiceImpl) beanFactory
.getBean("petService");
}
}


结果:

ItemDaoImpl的构造方法!
PetDaoImpl的构造方法


- 通过构造方法注入:

package DaoImpl;

public class FindAll {
public FindAll(){
System.out.println("2findAll Constructor");
}
}


package ServiceImpl;

import DaoImpl.FindAll;

public class FindAllService {
private FindAll find;
public FindAllService(FindAll find) {
this.find = find;
System.out.println("1findAllService Constructor!");
}
}


配置:

<?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="findALlService" class="ServiceImpl.FindAllService">
<constructor-arg name="find" ref="findAll"></constructor-arg>
</bean>
<bean id="findAll" class="DaoImpl.FindAll"></bean>
</beans>


测试:

public class test01 {
public static void main(String[] args) {
BeanFactory beanFactory=new ClassPathXmlApplicationContext("spring.xml");
FindAllService find=(FindAllService) beanFactory.getBean("findALlService");
}
}


测试结果:

2findAll Constructor
1findAllService Constructor!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: