zoukankan      html  css  js  c++  java
  • IOC控制反转

    控制反转:

       把对象的创建交给spring容器来做

          spring容器创建对象的方式

             1、默认是调用默认的构造函数

             2、利用静态工厂方法创建

                 spring调用工厂方法产生对象,但是真正创建对象还是由程序员来完成的

             3、实例工厂方法

            说明:

               spring配置文件中,只要是一个bean就会为该bean创建对象

          spring容器创建对象的时机

            在单例的情况下

             1、在默认的情况下,启动spring容器创建对象

             2、在spring的配置文件bean中有一个属性lazy-init="default/true/false"

                   1、如果lazy-init为"default/false"在启动spring容器时创建对象

                   2、如果lazy-init为"true",在context.getBean时才要创建对象

               意义:

                    在第一种情况下可以在启动spring容器的时候,检查spring容器配置文件的正确性,如果再结合tomcat,

                    如果spring容器不能正常启动,整个tomcat就不能正常启动。但是这样的缺点是把一些bean过早的放在了

                    内存中,如果有数据,则对内存来是一个消耗

                    在第二种情况下,可以减少内存的消耗,但是不容易发现错误

            在多例的情况下

                就是一种情况:在context.getBean时才创建对象

          spring的bean中的scope

            1、由spring产生的bean默认是单例的

            2、可以在spring的配置文件中,scope的值进行修改="singleton/prototype/request/session/global session"

            3、如果spring的配置文件的scope为"prototype",则在得到该bean时才创建对象

          spring容器对象的生命周期:

            1、spring容器创建对象

            2、执行init方法

            3、调用自己的方法

            4、当spring容器关闭的时候执行destroy方法

                                     ClassPathXmlApplicationContext.destroy()或者ClassPathXmlApplicationContext.destroy()

      当scope为"prototype"时,spring容器是否调用destroy方法?

            

    首先

    applicationContext.xml下

     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     xsi:schemaLocation="http://www.springframework.org/schema/beans
     5            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     6     <import
     7         resource="cn/itcast/spring/sh/ioc/createobject/applicationContext-createobject.xml" />
     8     <import resource="cn/itcast/spring/sh/ioc/alias/applicationContext-alias.xml" />
     9     
    10     <import resource="cn/itcast/spring/sh/createobject/method/applicationContext-createobject-method.xml" />
    11     
    12 <!--     <import resource="cn/itcast/spring/sh/createobject/when/applicationContext-createobejct-when.xml" /> -->
    13     
    14 <!--     <import resource="cn/itcast/spring/sh/scope/applicationContext-scope.xml" /> -->
    15     
    16 <!--     <import resource="cn/itcast/spring/sh/initdestroy/applicationContext-initdestroy.xml" /> -->
    17     
    18 <!--     <import resource="cn/itcast/spring/sh/di/set/applicationContext-di-set.xml" /> -->
    19     
    20 <!--     <import resource="cn/itcast/spring/sh/di/constructor/applicationContext-di-constructor.xml" /> -->
    21     
    22 <!--     <import resource="cn/itcast/spring/sh/document/applicationContext-document.xml" /> -->
    23 
    24 <!--     <import resource="cn/itcast/spring/sh/mvc/applicationContext-mvc.xml"/> -->
    25 </beans>
    View Code

    spring创建对象的几种方式:

    1.bean默认构造函数 以及别名的使用

    cn.itcast.spring.sh.ioc.alias包下

     1 package cn.itcast.spring.sh.ioc.alias;
     2 
     3 public class HelloWorld {
     4     public HelloWorld(){
     5         System.out.println("new instance");
     6     }
     7     public void hello(){
     8         System.out.println("hello");
     9     }
    10 }
    View Code

    applicationContext-alias.xml

     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        xsi:schemaLocation="http://www.springframework.org/schema/beans
     5            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     6       <bean id="helloWorld_Alias" class="cn.itcast.spring.sh.ioc.alias.HelloWorld"></bean>
     7       <!-- 
     8           name的属性和bean的id对应
     9        -->
    10       <alias name="helloWorld_Alias" alias="王二麻子"/>
    11 </beans>

    测试:

     1     @Test
     2     public void testAlias(){
     3         /**
     4          * 别名
     5          * 1、启动spring容器
     6          * 2、从spring容器中把对象取出来
     7          * 3、对象调用方法
     8          */
     9         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    10         cn.itcast.spring.sh.ioc.alias.HelloWorld helloWorld = (cn.itcast.spring.sh.ioc.alias.HelloWorld)context.getBean("王二麻子");
        //当然可以用beanid创建
        //
    cn.itcast.spring.sh.ioc.alias.HelloWorld helloWorld = (cn.itcast.spring.sh.ioc.alias.HelloWorld)context.g    //etBean("helloWorld_Alias");
    11         helloWorld.hello();
    12     }
    13     

    2.静态工厂方法

     1 package cn.itcast.spring.sh.createobject.method;
     2 
     3 public class HelloWorld {
     4     public HelloWorld(){
     5         System.out.println("new instance");
     6     }
     7     public void hello(){
     8         System.out.println("hello");
     9     }
    10 }
    1 package cn.itcast.spring.sh.createobject.method;
    2 
    3 public class HelloWorldFactory {
    4     public static HelloWorld getInstance(){
    5         return new HelloWorld();
    6     }
    7 
    8 }

    applicationContext-createobject-method.xml

     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        xsi:schemaLocation="http://www.springframework.org/schema/beans
     5            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     6       <bean id="helloWorld_C_M" class="cn.itcast.spring.sh.createobject.method.HelloWorld" ></bean>
     7       <!-- 
     8           factory-method
     9             工厂方法
    10        -->
    11 <!--            利用静态工程方法 自动调用其构造的方法,主程序通过(HelloWorld)context.getBean("helloFactory")调用,返回HelloWorld对象 -->
    12     <bean id="helloFactory" class="cn.itcast.spring.sh.createobject.method.HelloWorldFactory" factory-method="getInstance"></bean>
    13     
    14 
    15 </beans>

    测试

     1     public void testCreateObjectMethod_StaticFactory(){
     2         /**
     3          * 1、启动spring容器
     4          * 2、从spring容器中把对象取出来
     5          * 3、对象调用方法
     6          */
     7         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     8         HelloWorld factory = (HelloWorld)context.getBean("helloFactory");
     9         factory.hello();
    10     }

    3.实例工厂方法

    HelloWorld.java

     1 package cn.itcast.spring.sh.createobject.method;
     2 
     3 public class HelloWorld {
     4     public HelloWorld(){
     5         System.out.println("new instance");
     6     }
     7     public void hello(){
     8         System.out.println("hello");
     9     }
    10 }
     1 package cn.itcast.spring.sh.createobject.method;
     2 
     3 public class HelloWorldFactory {
     4     public static HelloWorld getInstance(){      //用于静态工程方法
     5         return new HelloWorld();
     6     }
     7     
     8     public HelloWorld getIns(){          //用于实例工厂方法
     9         return new HelloWorld();
    10     }
    11 }

    applicationContext-createobject-method.xml

     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        xsi:schemaLocation="http://www.springframework.org/schema/beans
     5            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     6       <bean id="helloWorld_C_M" class="cn.itcast.spring.sh.createobject.method.HelloWorld" ></bean>
     7       <!-- 
     8           factory-method
     9             工厂方法
    10        -->
    11 <!--            利用静态工程方法 自动调用其构造的方法,主程序通过(HelloWorld)context.getBean("helloFactory")调用,返回HelloWorld对象 -->
    12 <!--     <bean id="helloFactory" class="cn.itcast.spring.sh.createobject.method.HelloWorldFactory" factory-method="getInstance"></bean> -->
    13     
    14 <!--     实例工程调用范围 -->
    15     
    16     <bean id="helloFactory" class="cn.itcast.spring.sh.createobject.method.HelloWorldFactory" ></bean>
    17 <!--         这种调用方式就是非静态了,先实例工厂对象通过factory-bean注入,然后 调用getIns方法主程序HelloWorld factory = (HelloWorld)context.getBean("aa"); -->
    18     <bean id="aa" factory-bean="helloFactory" factory-method="getIns"></bean>
    19 </beans>

    测试

     1     @Test
     2     public void testCreateObjectMethod_Factory(){
     3         /**
     4          * 1、启动spring容器
     5          * 2、从spring容器中把对象取出来
     6          * 3、对象调用方法
     7          */
     8         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     9         HelloWorld factory = (HelloWorld)context.getBean("aa");
    10         factory.hello();
    11     }
  • 相关阅读:
    阿里云SLB的http强制转https
    nginx反向代理springboot的jar包
    阿里云Centos7上添加swap分区
    AWS云怎么删除信用卡账户
    Linux记录别人操作
    Jumpserver里常用的sudo权限
    端口一览表
    网络端口及其详解
    阿里云安全防坑指南
    LINUX添加只读用户(查日志专用)
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3782692.html
Copyright © 2011-2022 走看看