Spring-IOC

来源:互联网 发布:如何在网络上做推广 编辑:程序博客网 时间:2024/06/07 07:05

Spring是一个非常活跃的开源框架;它是一个基于Core来构架多层JavaEE系统的框架,它的主要目地是简化企业开发.

Spring以一种非侵入式的方式来管理你的代码,Spring提倡”最少侵入”,这也就意味着你可以适当的时候安装或卸载Spring 




配置文件

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"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">   <!--    beans      把一个类放入到spring容器中,该类就称为bean    -->    <!--     一个bean就代表一个类      id就是唯一标识符     -->   <bean id="helloWorld" class="cn.itcast.spring0909.helloworld.HelloWorld"></bean></beans>


<?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-2.5.xsd">    <!--     在这个配置中,spring容器要用默认的构造函数为HelloWorld创建对象     --><bean id="helloWorld" class="cn.itcast.spring0909.createobject.HelloWorld"></bean><!-- 采用静态工厂方法创建对象factory-method为工厂方法 --> <bean id="helloWorld2" class="cn.itcast.spring0909.createobject.HelloWorldFactory" factory-method="getInstance"></bean></beans>

<?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-2.5.xsd"><bean id="helloWorld" class="cn.itcast.spring0909.alias.HelloWorld"></bean><alias name="helloWorld" alias="王二麻子"/><alias name="helloWorld" alias="林志玲"/><alias name="helloWorld" alias="里活命"/></beans>

<?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-2.5.xsd">           <!--            在启动spring容器的时候,spring容器配置文件中的类就已经创建完成对象了            lazy-init               default  false               true  在context.getBean的时候才要创建对象                  *  优点                                如果该bean中有大数据存在,则什么时候context.getBean,什么时候创建对象                                可以防止数据过早的停留在内存中,做到了懒加载                  *  缺点                                 如果spring配置文件中,该bean的配置有错误,那么在tomcat容器启动的时候,发现不了               false 在启动spring容器的时候创建对象                  *  优点                                 如果在启动tomcat时要启动spring容器,                                 那么如果spring容器会错误,这个时候tomcat容器不会正常启动                  *  缺点                                  如果存在大量的数据,会过早的停留在内存中            -->   <bean id="helloWorld" class="cn.itcast.spring0909.createobject.when.HelloWorld" lazy-init="true"></bean>   <bean id="person" class="cn.itcast.spring0909.createobject.when.Person" lazy-init="true" ></bean></beans>


<?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-2.5.xsd">  <!--   在默认情况下,spring创建bean是单例模式   scope      singleton  默认         单例         属性是共享的         一般情况下,把数据存放在方法中的变量中      prototype          多例          当一个bean是多例模式的情况下,lazy-init为false或者default无效   -->  <bean id="helloWorld"  class="cn.itcast.spring0909.scope.HelloWorld" scope="prototype" lazy-init="false"></bean></beans>

<?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-2.5.xsd">           <!--            init-method             * 该方法是由spring容器执行             * 在构造函数之后执行             * 如果在构造函数之后,在调用方法之前要做一些工作,可以在init方法中完成           destroy-method             * 如果该bean是单例,则在spring容器关闭或者销毁的时候,执行该方法             * 如果该bean是多例,则spring容器不负责销毁           说明:要想让spring容器控制bean的生命周期,那么该bean必须是单例                      如果该bean是多例,该bean中还有资源,关闭资源的操作由程序员完成            --> <bean id="helloWorld" class="cn.itcast.spring0909.initdestroy.HelloWorld" scope="prototype" init-method="init" destroy-method="destroy"></bean></beans>

spring的IOC:

  * IOC:spring容器控制对象的生命周期:前提条件:在spring容器中的bean必须是单例的

       * 创建

          * 方式

             * 利用默认的构造函数,如果没有默认的构造函数,会报错

             * 利用静态工厂方法

             * 利用实例工厂方法

          * 时机

             * lazy-init为“default/false”当启动spring容器的时候创建bean

                 但是如果该bean是prototype时,特殊。这种情况无效

                  *  在spring容器启动的时候,就会发现错误

                  *  有可能会造成一些数据长时间驻留在内存中

             * lazy-init为"true"当context.getBean时创建

                  bean为多例时,必须用这种方案创建对象

                  *  不能及时发现错误

                  *  数据会在需要的时候加载

       * 初始化

            * 由spring容器调用init方法

            * 在构造函数之后执行

       * 销毁

            * 如果是单例,则必须返回ClassPathXmlApplicationContext该容器,才能执行销毁工作

            * 如果是多例,容器不负责销毁


0 0
原创粉丝点击