spring - spring's IOC and DI (the core idea of Spring)

What is a frame:

    A framework is a modular integration of some repetitive and important code. It is convenient to call directly in the future, in order to improve the development efficiency.

        SSH: sturs2 spring hibernate

        SSM:SpringMVC Spring Mybatis

Spring Framework:

The main purpose of     Spring is to simplify the development of code , and can integrate all the mainstream frameworks at this stage , acting as a glue. Using Spring in the project can be a one-stop development and programming. Programming efficiency is greatly improved. Among them, IOC , DI and AOP are the two core technologies of Spring .

Spring's IOC:

    IOC is called inversion of control , which is to reverse the right to create an object. Before creating an object manually, it now becomes a new object created with the help of the Spring container . From then on the programmer does not need to pay attention to the creation process of the object. No need to pay attention to the life cycle of the object.

    Peron  p1 = new Person(); -----> do it manually

    Person  p2 = spring container.get***(); -----> Created by spring container


Implementation steps of Spring's IOC:

    0  Import the jar package:

                                             

    1. Create an entity class: As long as a bean is managed by spring, it is a bean

                                                

    2. Write the configuration file ( 1. Write the header of the configuration file      2. Write the entity content of the configuration file)

        The name of the configuration file: It is better to use the spring default name  application Context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
 
    <!--id="Uniquely identifies this bean" The id must not be repeated
        The naming rules of id: the first letter is lowercase, other camel case rules PersonService
        class is the specific path of the object
    -->
    <bean id="hello" class="bean.Hello"></bean>
 
</beans>

    3. Start the Spring container: Get the object from the container and call the method to complete the corresponding function.

                              

The principle of Spring object creation and acquisition:

    When the spring container starts, it needs to parse the configuration file. The configuration file creates an object in the form of line-by-line parsing. According totheclassbeancreated by reflection call and injected into thespringcontainer. Springinternallystores the generated objects in the form ofmapsThekeymapistheIDbean, and the value in the mapis the created object.

Spring's mechanism for creating objects:

    If any error occurs while creating the object, the entire spring container will not be started.

How to get objects in Spring:

                                        

    1. Get the object by the ID of the bean :

        context.getBean("hello");   where hello represents the ID of the bean

     2. Get the object through the class type: context.getBean(Hello.class);    the parameter in it indicates the class type

        Note: Although the object can be obtained in two ways, it is recommended that you use the id to obtain it. If multiple beans with the same class type are configured in the xml , an error will be reported.

                                                    

Alias ​​tag alias:

<!--Alias ​​tag
    The ID of the bean whose alias is written in the name attribute
    Alias ​​can be both Chinese and English
 -->
<alias name="hello" alias="hello1610班"/>

    If you want to give a bean more than one name you need to use the alias tag.


How to create an object:

    1. The default constructor creates an object (the most used)

        The default way to create objects in spring is to create objects through no-parameter construction, so when writing construction methods in the future, you must add no-parameter construction to ensure the correct operation of the program.

     2.通过静态工厂创建对象(必须要有静态方法)

                                            

            静态工厂中,最为关键的部分就是静态方法,如果没有静态方法,那么必然报错。

            说明:通过静态方法创建对象,和通过类名.static方法 的方式一致。

 3.实例工厂创建对象--先实例工厂,再创建对象

            

<!--通过实例工厂创建对象  -->
<bean id="newInstanceFactory" class="factory.NewInstanceFactory"></bean>
<!--     factory-bean="生产对象的工厂" 
         factory-method="工厂类中的方法"
        和对象.方法名类似
-->
<bean id="calendar2" factory-bean="newInstanceFactory" factory-method="getCalendar"></bean>

 4.spring工厂创建对象

    1.spring工厂必须实现FactoryBean<T>接口其中泛型T表示生产的对象

    2.实现方法

            

    3.配置文件中的配置

<!--通过spring工厂创建对象
class="spring工厂的类型"
  -->
<bean id="calendar3" class="factory.SpringFactory"></bean>


spring创建对象的单例和多例scope:

    spring通过容器创建对象时,默认的都是单例的。在spring容器中对象只存在一份,如果使用该对象直接通过getBean()方法获取。

        1.在默认条件下对象是单例的

        2.scope="singleton"  强制是单例的

        3.scope="prototype"  代表多例对象

 

懒加载:

    问题:由于spring在启动时会逐行解析,为每一个bean都会创建对象,这样的方式不是特别高效。

    如果容器启动时只创建必须要使用的对象,而其他对象当调用getBean方法时才会被创建,这样的模式更加的高效

    懒加载:当什么时候调用getBean方法时对象才会被创建。

         说明:在默认条件下spring的懒加载不生效。

            lazy-init="false"  懒加载不生效

            lazy-init="true"  懒加载生效

            default-lazy-init="false"    全局的懒加载配置,如果为false则懒加载不生效,如果为true则全部的bean都懒加载

            lazy-init="default"  应该与全局变量配置保持一致

             如果全局变量与lazy-init不一致时以lazy-init为准


scope遇到lazy-init时的问题(多例就是懒加载):

    lazy-init="false"  scope="prototype" 设置懒加载不生效,scope为多例的。这时的配置lazy-init将不起任何效果,该配置必然懒加载。(因为spring只负责创建多例,不负责管理多例,所以多例的时候必然懒加载)

    在Spring中对象只维护单例对象,只负责单例对象的生命周期,如果对象为多例时,spring只管创建该对象交给用户使用后将不再管理该对象,让该对象自生自灭。

    注意:Spring只管单例不管多例,多例对象创建统统懒加载


自定义初始化和销毁方法:

    init-method="自定义方法的名称"   自定义的初始化方法

    destroy-method="自定义销毁方法的名称"   自定义的销毁方法,spring在关闭的时候就会调用销毁方法。

                            

容器中对象的生命周期:

    1.调用构造方法创建对象

    2.调动init方法初始化属性

    3.调用目标方法完成相应的功能

    4.调用销毁方法做收尾工作。



DI(依赖注入):

    依赖注入一般注入可以是基本类型、字符串、对象的引用、集合(List,Set,Map)

    注入的方式

        1.set方法注入

                                    

        2.map封装时使用linkedHashMap进行封装,是有序的

        3.为引入类型赋值

            通过ref可以为引用类型进行赋值,name是新建的那个引用的名字ref的值写的是beanID

                                    



说明:SpringIOC和DI的意义,使用了SpringIOC和DI能够在最大程度上减少代码的耦合性,并且扩展性特别的强。





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325519642&siteId=291194637