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方法
- 配置init-method
销毁:(两种方式)
- 实现org.springframework.beans.factory.DisposableBean接口,覆盖destroy方法
- 配置destroy-method
配置全局默认初始化、销毁方法
(四) 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接口向应用程序屏蔽了资源表现形式的多样性
ResourceLoader接口
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); }
相关文章推荐
- Java 接口和抽象类区别
- android-Ultra-Pull-To-Refresh Dem实例 Eclipse中编译的办法
- 【Spring学习笔记-MVC-11--】Spring MVC之表单标签
- XStream xml与java对象互相转换,自动识别java
- 启动eclipse错误:Failed to write core dump
- JSP-Java Server Page基础知识
- SpringMVC+Mybatis整合的增删改查
- javaweb入门实例---servlet例子
- 【Spring】第一章 简单示例
- {嵌入式学习笔记(一)}ubuntu下嵌入式开发环境搭建
- java中的sleep()和wait()的区别
- JAVA多线程编程(三)——线程的生命周期(sleep,join,interrupt)
- JVM学习笔记(二)------Java代码编译和执行的整个过程
- JVM学习笔记(二)------Java代码编译和执行的整个过程
- Spring整合RMI
- 错误笔记:Caused by: java.lang.StackOverflowError
- java类的加载以及初始化顺序
- Spring MVC使用拦截器实现权限控制
- QA:java.lang.RuntimeException:java.io.FileNotFoundException:Resource nexus-maven-repository-index.properties does not exist.
- R语言RJava安装步骤