zoukankan      html  css  js  c++  java
  • Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)

    目录

      1 什么是spring框架

      2 spring框架的特点

      3 spring容器

        3.1 什么是spring容器

        3.2 spring容器创建对象的编程步骤

        3.4 spring容器创建对象的方式

        3.5 bean元素的几个重要属性

      4 IOC

        4.1 什么是IOC

        4.2 什么事DI

        4.3 DI的三种方式

    1 什么是spring框架

      是一个开源的用来简化企业级应用开发的框架

    2 spring框架的特点

      2.1 简化开发

        spring对一些常见的api(比如jdbc)做了封装,使用这些封装之后的api,代码会大大简化
      比如,使用springjdbc来访问数据库,就不用考虑如何获取连接,关闭连接等操作。

      2.2 管理对象

        spring可以帮我们管理对象之间的依赖关系,这样一来, 软件更容易维护。

      2.3 集成其它框架

        spring可以将一些框架集成进来,更方便使用这些框架。、
      比如,可以利用spring集成mybatis(mybatis是一个用 来访问数据库的框架),这样mybatis用起来更加简单。 

    3 spring容器

      3.1 什么是spring容器

        spring框架当中的一个核心模块,用来管理对象。

      3.2怎么利用 spring容器 来创建对象

        3.2.1 创建一个 maven项目

          》记得让maven项目中出现 web.xml 这个配置文件 -->> 还记得咋整吗?

        3.2.2 导包

          spring-webmvc -->> 启动 spring容器 时需要用到
          junit -->> 进行单元测试时需要用到

        3.2.3 启动 spring容器

          》添加一个 spring容器 配置文件

            例:给Student类配置bean,只需在spring的配置文件中添加

              <bean id="stu" class="test.Student"></bean>

                id : 必须要保证唯一

                class:就是需要配置bean的类名,但是必须在前面加上 包名.
          》利用 ApplicationContext 的实现类 ClassPathXmlApplicationContext 去启动容器

        3.2.4 利用 getBean(String name, Class<T> requiredType) 来实例化对象

          注意:spring容器会利用相关类的无参构造器去创建实例,所以相关类中必须要有无参构造器,否则会报错:“找不到无参构造器”

           

      3.3 注意

        spring容器一旦启动,就会在 堆 中将所有配置了 bean 的类创建好一个实例

     1 package test;
     2 
     3 import java.io.Serializable;
     4 
     5 public class Student implements Serializable {
     6     private Integer id;
     7     private String name;
     8     private String gender;
     9     
    10     
    11     public Student() {
    12         super();
    13         System.out.println("New Student()");
    14     }
    15     public Integer getId() {
    16         return id;
    17     }
    18     public void setId(Integer id) {
    19         this.id = id;
    20     }
    21     public String getName() {
    22         return name;
    23     }
    24     public void setName(String name) {
    25         this.name = name;
    26     }
    27     public String getGender() {
    28         return gender;
    29     }
    30     public void setGender(String gender) {
    31         this.gender = gender;
    32     }
    33     
    34     public String toString() {
    35         return "Student [id=" + id + ", name=" + name + ", gender=" + gender + "]";
    36     }
    37     
    38 }
    Student类
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" 
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context" 
     5     xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
     6     xmlns:jee="http://www.springframework.org/schema/jee" 
     7     xmlns:tx="http://www.springframework.org/schema/tx"
     8     xmlns:aop="http://www.springframework.org/schema/aop" 
     9     xmlns:mvc="http://www.springframework.org/schema/mvc"
    10     xmlns:util="http://www.springframework.org/schema/util"
    11     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    12     xsi:schemaLocation="
    13         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    14         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    15         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    16         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    17         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    18         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    19         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    20         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    21         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    22     
    23     <bean id="stu" class="test.Student"></bean>
    24     
    25 </beans>
    test.xml配置文件
     1 package test;
     2 
     3 import java.io.Serializable;
     4 
     5 import org.springframework.context.ApplicationContext;
     6 import org.springframework.context.support.ClassPathXmlApplicationContext;
     7 
     8 public class Test implements Serializable {
     9     public static void main(String[] args) {
    10         ApplicationContext ac = new ClassPathXmlApplicationContext("test.xml");
    11         System.out.println(ac);
    12         
    13         Student stu1 = ac.getBean("stu", Student.class);
    14         System.out.println(stu1);
    15     }
    16 }
    Test类

      3.4spring容器 实例化对象的三种方法

        3.4.1 利用无参构造器实现(很重要) 

          必须有无参构造器,如果已经添加了有参构造器,那么默认的无参构造器就会被覆盖;此时就需要手动添加一个无参构造器

        3.4.2 利用静态工厂方法实现(了解)

        3.4.3 利用实例化工厂方法实现(了解)

       3.5 bean元素的几个重要属性

        3.5.1 作用域

          scope 属性 : 用来指定作用域

          缺省值是 singleton(单例),如果值是prototype(原型),则可以创建多个对象,否则不可以

          我们一般使用 单例模式 就可以啦(即:默认值)

        3.5.2 生命周期

          初始化

            init-method属性:指定初始化方法

          销毁

            destroy-method属性:指定销毁方法

            只有作用域为单例的 bean, 销毁方法才有效

            必须使用这个spring容器接口:AbstractApplicationContext

        3.5.3 延迟加载

          默认情况下当 spring容器 启动之后,会将所有作用域为单例的 bean 都创建好

          lazy-init属性:指定是否延迟加载,值为 true 时延迟加载(一般不用延迟加载)

    4 IOC(Inversion Of Controll 控制反转)

      4.1 什么事IOC

        对象之间的依赖关系交给容器来管理。

      4.2 什么是DI (Dependency Injection 依赖注入)

        容器通过调用set方法或者构造器来建立对象之间的依赖关系

        注:IOC是目标,而DI是手段

      4.3DI 注入的两种方式

        4.3.1 利用 set 方法完成依赖注入(掌握)

          注入类中必须有一个成员变量,该成员变量的类型必须是被依赖注入的类

          注入类中必须实现需要注入的那个成员变量的 set 方法

          set 方法进行依赖注入时,在配置文件中用到的是 property标签;如果需要注入的类中有有参构造器,那么必须实现无参构造器,因为有参构造器会覆盖无参构造器(注意:菜鸟一般吧无参构造器和有参构造器都实现)

          注意:注入类中那个成员变量的类型 一般都设定为被注入类的一个接口,这样有利于今后进行维护

          利用 property标签实现依赖注入

            <property name="stu" ref="student01"></property>

              name:需要依赖注入的成员变量

              ref:需要依赖注入类的id属性值

          

          图解:创建A的实例,而且B是注入到A中的;B类中必须实现无参构造器,A中必须添加一个类型为B的成员变量,而且还必须为该成员变量实现set方法,在spring配置文件中配置A和B的bean,而且在A的bean中还要利用property标签来实现B的依赖注入

        4.3.2 利用有参构造器完成依赖注入(掌握)

          这里的构造器是有参构造器,但是它的参数这是你需要注入的参数名,不包含其他的

          利用constructor-arg标签实现依赖注入

            <constructor-arg index="0" ref="b1"/>

              index:有参构造器中参数的位置,从0开始

              ref:依赖注入类的id属性值

          

          图解:创建A的实例,而且B是注入到A中的;B类中必须实现无参构造器,A中必须添加一个类型为B的成员变量;为A添加一个有参构造器,而且该有参构造器的参数只是你要注入的那个成员变量,不包含其他的;而且该有参构造器任然会覆盖无参构造器,所以如果想要使用无参构造器就必须先实现无参构造器;在spring的配置文件中配置A和B的bean,而且还需要用constructor-arg标签实现B的依赖注入

        4.3.3 自动装配(一般不用)

        注意:依赖注入一般都由注释实现

    源代码链接:点击前往

  • 相关阅读:
    【正视CSS 03】block与position,出门在外的朋友端午节快乐
    【正视CSS 07】再看verticalalign
    【正视CSS 06】构建我们自己的世界观!
    【正视CSS 02】inline与lineheight、float、absolute的故事
    【初窥javascript奥秘之闭包】叶大侠病都好了,求不踩了:)
    【javascript面向对象之路】让我们一起来坦克大战吧01
    Python学习之三:《Dive in Python》学习笔记二
    恢复GMail选择栏(All None Read Unread….)的油猴(GreaseMonkey)脚本
    群聊天时学习到的两个JS知识(变量范围,Foreach顺序)
    利用宏自动附加到WebServer进程
  • 原文地址:https://www.cnblogs.com/NeverCtrl-C/p/6849543.html
Copyright © 2011-2022 走看看