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

Spring学习(4)---Bean基础

2015-06-26 15:38 645 查看
Bean配置项

Bean的作用域

Bean的生命周期

Bean的自动装配

Resources & ResourceLoader

(一) Bean配置项

常用的配置项

Id (IOC容器中Bean的唯一标识)

Class (具体要实例化的类) (必须要有的)

Scope (Bean的作用域)

Constructor arguments (构造器的参数)

Properties (属性)

Autowiring mode (自动装配模式)

lazy-initialzation mode (懒加载模式)

Initialzation/destrustion method (初始化/销毁方法)

(二) Bean的作用域

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

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

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

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

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

(三) Bean的生命周期

定义 -- Spring bean 配置文件中配置的bean(id,及对应的class)

初始化

使用

销毁

初始化:(两种方式)

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

public class ExampleBean implements InitializingBean{

@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub

}

}


- 配置init-method

<bean id="exampleInitBean" class="com.ioc.ExampleBean" init-method="init"></bean>

-----------------------------------------------------------------------------------------
public class ExampleBean {

public void init(){
//your work code
}
}


销毁:(两种方式)

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

public class ExampleBean implements DisposableBean{

@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
}

}


- 配置destroy-method

<bean id="exampleDestoryBean" class="com.ioc.ExampleBean" destroy-method="destory"></bean>
---------------------------------------------------------------------------------------------
public class ExampleBean{
public void destory(){
//your work code
}
}


配置全局默认初始化、销毁方法

<?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-4.1.xsd" default-init-method="init"      default-destroy-method="destroy">

</beans>


(四) Bean的自动装配(Autowiring)

No :不做任何操作

byname : 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配

byType : 如果容器存在一个与指定属性类型相同的bean,那么将与该属性自动装配;如果存在多个该类型的bean,那么抛出异常,并指出不能使用byType方式进行自动装配;如果没有找到相匹配的bean,则什么事都不发生

Construtor : 与byType方式相似,不同之处在于它应用于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么会抛出异常

(五) Resources & ResourceLoader

针对资源文件的统一接口

Resources

- UrlResource : URL对应的资源,根据一个URL地址即可构建

- ClassPathResource :获取类路径下的资源文件

- FileSystemResource :获取文件系统里面的资源

- ServletContextResource :ServletContext封装的资源,用于访问ServletContext环境下的资源

- InputStreamResource :针对于输入流封装的资源

- ByteArrayResource :针对于字节数组封装的资源

ResourceLoader

所有的ApplicationContext都实现了ResourceLoader接口,实现对Resources加载

PS:

Resource接口

Spring将所有形式的资源表现概括成一个Resource接口(简化),Resource接口向应用程序屏蔽了资源表现形式的多样性

public interface Resource {

InputStream getInputStream();

URL getURL();

File getFile();

boolean exists();

}


ResourceLoader接口

public interface ResourceLoader {

Resource getResource(String location);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: