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

IOC

提纲–

  1. 接口及面向接口编程
  2. 什么是IOC
  3. Spring 的Bean配置
  4. Bean的初始化
  5. Spring的常用注入方式

接口

  1. 用于沟通的中介物的抽象化
  2. 实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式。
  3. 对应Java接口即声明,声明了那些方法是对外公开提供的
  4. 在Java8中,接口可以拥有方法体。

面向接口编程

  1. 结构设计中,分清层次及调用关系,每层只向外(上层)提供一组功能接口,各层间仅依赖接口而非实现类
  2. 接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要
  3. “面向接口编程”中的“接口”是用于影藏具体实现和实现多态性的组件

什么是IOC

  1. IOC:(Inversion of Control)控制反转是Spring的核心,贯穿始终。所谓IoC,对于spring框架来说,就是有Spring来负责控制对象的生命周期和对象键的关系。
    –传统开发模式:对象之间相互依赖
    – IoC开发模式:IoC容器安排对象之间的依赖。

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

  3. 目的:
    创建对象并且组成对象之间的关系

IoC的好处

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

  • 降低组件之间的耦合对
  • 提高开发效率和产品质量
  • 统一标准,提高模块的复用性
  • 模块具有热插拔特性

IoC通俗的理解如下:

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

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


Bean容器初始化

  1. 文件:
    – FileSystemXlmApplicationContext
  2. Classpath:
    – ClassPathXmlApplicationContext
  3. Web应用:

两种依赖注入的方式:

  1. 设值注入
  2. 构造注入

  • 通过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!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值