您的位置:首页 > 编程语言 > Java开发

Spring笔记之二 -- SpringIOC(控制反转)

2014-08-02 00:11 465 查看
Spring的控制反转(IOC):把对象的创建、初始化、销毁等工作交给spring容器来做。由spring容器控制对象的生命周期。

步骤:

A. 启动spring容器

1、 在类路径下寻找配置文件来实例化容器

ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});可以在整个类路径中寻找xml文件

* 通过这种方式加载。需要将spring的配置文件放到当前项目的classpath路径下

* classpath路径指的是当前项目的src目录,该目录是java源文件的存放位置。

2、 在文件系统路径下寻找配置文件来实例化容器

ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});Spring的配置文件可以指定多个,可以通过String数组传入。

注:经常用第一种方法启动容器

B. 从spring容器中提取对象

1.1 别名

<beans>

<alias name="person" alias="p"/>

<bean name="person" class="cn.itcast.aliasspring.Person"/>

</beans>

通过这样的配置,可以达到在一个地方命名,在多个地方使用不同的名字的效果。

1.2 Spring容器内部对象

1.2.1 创建对象的方式

1.2.1.1 无参构造函数

<bean id=“personService" class="cn.itcast.bean.impl.PersonServiceImpl"/>

1.2.1.2 静态工厂

<bean id="personService" class="com.itcast.factory.PersonServiceFactory" factory-method="createPersonService" />

public class PersonServiceFactory {

public static PersonService createPersonService(){

return new PersonServiceImpl();

}

}

1.2.1.3 实例工厂

1.2.2 对象的scope

1.2.2.1 singleton(默认值)

在每个Spring IoC容器中一个bean定义只有一个对象实例(共享)。

默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:

<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>

如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:

<beans default-lazy-init="true“ ...>

1.2.2.2 prototype

允许bean可以被多次实例化(使用一次就创建一个实例) . Spring不能对一个prototype bean的整个生命周期负责.这就意味着清楚prototype作用域的对象并释放任何prototype bean所持有的昂贵资源都是客户端的责任。

1.2.3 初始化bean时机

Spring默认在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初始化的一部分,ApplicationContext会自动创建并配置所有的singleton bean.通常情况下这是件好事。因为这样在配置中有任何错误能立即发现。

Lazy-init=”true or false”

Lazy-init 为false,spring容器将在启动的时候报错(比较好的一种方式)

Lazy-init 为true,spring容器将在调用该类的时候出错。

1.2.4 init、destroy

Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法。

<bean id=“foo” class=“...Foo”

init-method=“setup”

destory-method=“teardown”/>

当foo被载入到Spring容器中时调用init-method方法。当foo从容器中删除时调用destory-method(scope = singleton有效)

C、详细说明

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

补充:什么是单例 --> 在一个类中只能有一个实例,就是单例,经典的单例设计模式有懒汉式和饿汉式

* 创建 方式

* 利用默认的构造函数,如果没有默认的构造函数,会报错(spring容器调用默认的构造函数来创建对象的)

* 利用静态工厂方法

* 利用实例工厂方法

* 加载时机

* lazy-init为“default/false”当启动spring容器的时候创建bean,但是如果该bean是prototype时,特殊。这种情况无效

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

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

* lazy-init为"true"当context.getBean时创建bean为多例时,必须用这种方案创建对象

* 不能及时发现错误

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

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

* 初始化

* 由spring容器调用init方法

* 在构造函数之后执行

* 销毁

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

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

配置文件中解释加载的两种机制:

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

配置文件中解释对象的scope

<?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.spring.scope.HelloWorld" scope="prototype" lazy-init="false"></bean>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: