Spring学习笔记[2] -- IOC快速入门

IOC 概述

IOC:Inverse of Control,控制反转,指的是对象的创建权反转(交给)Spring框架,其作用是实现了程序的解耦合

IOC的实现原理

传统方式

面向接口编程,资源创建的权力由App决定,获取资源的时候需要new一个新对象,在切换底层类的实现的时候,需要修改程序的源码
这里写图片描述

IOC控制反转

将控制权移交给Spring,通过工厂+反射+配置文件来实现解耦合

这里写图片描述

入门程序

创建 接口

public interface UserService {
    public void sayHello();
}

编写接口 实现类

public class UserServiceImpl implements UserService {
    public void sayHello(){
        System.out.println("Demo01: Hello Spring ");
    }
}

约束的配置文件

applicationContest.xml中使用bean标签

<bean id="userService" class="gongfukangee.Demo01.UserServiceImpl"/>

创建测试类

public class Demo01Test {
  /**
     * @Auther gongfukang
     *原来的方法
     */
    @org.junit.Test
    public void run1(){
        //创建实现类
        UserServiceImpl userService=new UserServiceImpl();
        userService.sayHello();
    }

      /**
     * @Auther gongfukang
     * Spring-IoC 
     */
    @org.junit.Test
    public void run2(){
        // 创建工厂,加载核心配置文件
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        // 从工厂中获得对象,与在XML bean 标签使用的 id 一致
        UserServiceImpl userService=(UserServiceImpl) applicationContext.getBean("userService");
        // 调用对象的方法执行
        userService.sayHello();
    }
}

Bean标签的配置

  • id属性:Bean标签的名字,在约束中唯一,取值要求:必须以字母开始,不能出现特殊字符
  • name属性:Bean标签的名字,没有采用ID的约束
  • class属性:Bean对象的全路径
  • scope属性:scope属性代表Bean的作用范围
    • singleton:单例(默认)
    • prototype:多例
    • request:在Web项目中,每次HTTP请求都会创建一个新的Bean
    • session:在Web项目中,同一个HTTP Session共享一个Bean
    • globalsession:在Web项目中,多服务器间的session
  • Bean对象的创建和销毁两个属性配置
    • init-method:当Bean被载入到容器的时候调用init-method属性指定的方法
    • destroy-method:当Bean从容器中删除的时候调用destroy-method属性指定的方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值