Spring IoC 和 DI


1.IoC (Inversion of Control 中文控制反转)
 是一种重要的面向对象编程的法则来削减计算机程序的耦合问题。意思是将对象的创建反转给Spring

2.Ioc的两种类型
 2.1依赖注入(DI)
  创建Web项目,导入jar包


所需jar包

 2.2编写代码
UserService.java

package com.wuhaitao.spring.demo1;

/**
* @author wuhaitao
* 用户管理业务层接口
*/
public interface UserService {
   public void save();
}

UserServiceImpl.java

package com.wuhaitao.spring.demo1;

/**
 * @author wuhaitao
 *  用户管理业务层实现类
 */
public class UserServiceImpl implements UserService {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void save() {
        System.out.println("Userservice 执行了。。。。。" + name);
    }
}

UserServiceImplOther

package com.wuhaitao.spring.demo1;

public class UserServiceImplOther implements UserService {
    @Override
    public void save() {
        System.out.println("UserService另一种实现执行了。。。。。。");
    }
}

测试类 SpringTest.java

package com.wuhaitao.spring.demo1;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    /**
     * 传统方式 
     */
    @Test
    public void demo1() {
        UserService userService = new UserServiceImpl();
        userService.save();
    }
    
    /**
     * Spring方式
     */
    @Test
    public void demo2() {
        //创建Spring工厂
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //得到实例化对象
        UserService service = (UserService) applicationContext.getBean("userService");
        //利用多态调实现类方法
        service.save();
    }
}

applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 以上为Spring常用约束 -->
    
    <!-- Spring的入门配置  -->
    <bean id="userService" class="com.wuhaitao.spring.demo1.UserServiceImpl">
        <property name="name" value="邬海涛"></property>
    </bean>
</beans>

比较一下传统方式与Spring管理方式的区别
 当传统方式需要换一种实现时,例如UserServiceImplOther的实现类时,则源代码必须要改变,才能达到换一种实现的目的。而Spring管理方式,是通过 工厂 + 反射 + 配置文件 来实现程序解耦合的。


IoC的底层实现

当需要修改实现时,Spring方式只需修改Spring的配置文件就可以了,无需修改源代码。

2.2依赖查找 (一般不用)

3.DI(依赖注入)
 前提必须要有IoC的环境,Spring管理对象的创建时,将对象依赖的属性注入到对象当中。
 3.1依赖
  依赖是指在面向对象的时候,类A的方法中使用到了B类,这时需要将B类传入到A类当中,此时称A依赖了B。属性同样如此。


图片.png

通过配置,完成属性的注入。这个过程叫做DI

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.Spring简介 Spring是J2EE开发中一个很重要的框架。它主要用来解决下面两个问题。 解决大型软件开发...
    sixleaves阅读 1,382评论 0 6
  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 6,753评论 2 22
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 本文是我自己在秋招复习时的读书笔记,整理的知识点,也是为了防止忘记,尊重劳动成果,转载注明出处哦!如果你也喜欢,那...
    波波波先森阅读 12,330评论 6 86
  • 纵观整本书,我发现,这书是按照一个清晰的流程讲述的,这个流程是:1、提升对态度的认知(第一、第二章)2、自己如何获...
    闪电的蓝熊猫阅读 1,382评论 0 4