码迷,mamicode.com
首页 > 其他好文 > 详细

Bean

时间:2015-03-16 12:52:33      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

1. Bean配置项

1.1. ID

在整个IOC容器中Bean的唯一标识

1.2. Class

具体要实例化的类

1.3. Scope

范围,作用域

1.4. Constructor arguments

构造器的参数

1.5. Properties

属性

1.6. Autowiring mode

自动装配模式

1.7. lazy-initialization mode

懒加载模式

1.8. Initialization/destruction method

初始化和销毁方法

 

2. Bean的作用域

2.1. singleton

单例,指一个Bean容器中只存在一份

2.2. prototype

每次请求(每次使用)创建新的实例,destroy方式不生效

2.3. request

每次http请求创建一个实例且仅在当前request内有效

2.4. session

同上,每次http请求创建,当前session内有效

2.5. global session 

基于portlet的web中有效(portlet定义了globao session),如果是在web中,同session

 

3. Bean的生命周期

3.1. 定义:

3.2. 初始化

——实现org.springframework.beans.factory.InitializingBean接口,覆盖afterPropertiesSet方法

1 public class ExampleInitializingBean implements InitializingBean {
2     @Override
3     public void aferPropertiesSet() {
4         // do something
5     }
6 }

——配置init-method

1 <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init" />
1 public class ExampleBean {
2     public void init() {
3         // do some initialization work
4     }
5 }

3.3. 使用

3.4. 销毁

——实现org.springframework.beans.factory.DisposableBean接口,覆盖destroy方法

1 public class ExampleInitializingBean implements DisposableBean {
2     @Override
3     public void destroy() throws Exception {
4         // do something
5     }
6 }

——配置destroy-method

1 <bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup" />
public class ExampleBean {
    public void cleanup() {
        // do some destruction work (like releasing pooled connections)
    }
}

以上是针对某一Bean的处理方式,配置全局默认初始化、销毁方法

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/spring-beans.xsd"
5   default-init-method="init" default-destroy-method="destroy">
6 
7 </beans>

 

4. Bean的自动装配

 

5. Resources & ResourceLoader

Bean

标签:

原文地址:http://www.cnblogs.com/BlackList-Sakura/p/4341475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!