Spring知识整理(三)Spring之Bean之间的关系,Bean的作用域,Bean的生命周期
2018-04-03 16:21
573 查看
1(Bean之间的关系-继承,依赖)
•Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean•子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置•子 Bean 也可以覆盖从父 Bean 继承过来的配置•父 Bean 可以作为配置模板, 也可以作为 Bean 实例. 若只想把父 Bean 作为模板, 可以设置 <bean>的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean•并不是 <bean>元素里的所有属性都会被继承. 比如: autowire, abstract 等.•也可以忽略父 Bean 的 class 属性, 让子 Bean 指定自己的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true•Spring 允许用户通过 depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化之前创建好•如果前置依赖于多个 Bean,则可以通过逗号,空格或的方式配置 Bean 的名称
2(Bean的作用域)
•在 Spring 中, 可以在 <bean> 元素的 scope属性里设置 Bean 的作用域. •默认情况下, Spring 只为每个在 IOC 容器里声明的 Bean 创建唯一一个实例, 整个 IOC 容器范围内都能共享该实例:所有后续的 getBean() 调用和 Bean 引用都将返回这个唯一的Bean 实例.该作用域被称为 singleton, 它是所有 Bean 的默认作用域.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--spring使用bean来构造实例的时候默认使用的是单例作用域
使用scope来配置作用域
singleton:在整个容器的生命周期内只创建一个bean,是单例的,在容器初始化之前已经创建好了实例
prototype:原型的,容器初始化时不创建bean的实例,而在每次请求时都创建一个新的实例返回,也就是什么时候使用什么时候创建
request,session对应域对象,使用较少-->
<bean id="car" class="com.spring.beans.CarBean" scope="prototype">
<property name="name" value="保时捷"></property>
<property name="price" value="100000000"></property>
</bean>
<!--<bean id="car1" class="com.spring.beans.CarBean">-->
<!--<property name="name" value="保时捷"></property>-->
<!--<property name=
f997
"price" value="3000000"></property>-->
<!--</bean>-->
<bean id="car1" parent="car" p:name="奥迪"></bean>
<bean id="person" class="com.spring.beans.PersonBean" depends-on="car" ></bean>
</beans>
3(使用外部属性文件)
•在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和Bean 配置相分离•Spring 提供了一个 PropertyPlaceholderConfigurer的 BeanFactory后置处理器, 这个处理器允许用户将Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为${var}的变量, PropertyPlaceholderConfigurer从属性文件里加载属性, 并使用这些属性来替换变量.Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用
4(Bean的生命周期)
•Spring IOC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务. •Spring IOC 容器对 Bean 的生命周期进行管理的过程:–通过构造器或工厂方法创建 Bean 实例–为 Bean 的属性设置值和对其他 Bean 的引用–调用 Bean 的初始化方法–Bean 可以使用了–当容器关闭时, 调用 Bean 的销毁方法•在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法.•Bean 后置处理器允许在调用初始化方法前后对Bean 进行额外的处理.•Bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理, 而非单一实例. 其典型应用是: 检查 Bean 属性的正确性或根据特定的标准更改Bean 的属性.•对Bean 后置处理器而言, 需要实现
接口. 在初始化方法被调用前后, Spring 将把每个 Bean 实例分别传递给上述接口的以下两个方法:
•Spring IOC 容器对 Bean 的生命周期进行管理的过程:–通过构造器或工厂方法创建 Bean 实例–为 Bean 的属性设置值和对其他 Bean 的引用–将 Bean 实例传递给 Bean 后置处理器的 postProcessBeforeInitialization方法–调用 Bean 的初始化方法–将 Bean 实例传递给 Bean 后置处理器的 postProcessAfterInitialization方法–Bean 可以使用了–当容器关闭时, 调用 Bean 的销毁方法
•Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean•子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置•子 Bean 也可以覆盖从父 Bean 继承过来的配置•父 Bean 可以作为配置模板, 也可以作为 Bean 实例. 若只想把父 Bean 作为模板, 可以设置 <bean>的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean•并不是 <bean>元素里的所有属性都会被继承. 比如: autowire, abstract 等.•也可以忽略父 Bean 的 class 属性, 让子 Bean 指定自己的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true•Spring 允许用户通过 depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化之前创建好•如果前置依赖于多个 Bean,则可以通过逗号,空格或的方式配置 Bean 的名称
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="car" class="com.spring.beans.CarBean"> <property name="name" value="保时捷"></property> <property name="price" value="100000000"></property> </bean> <!--<bean id="car1" class="com.spring.beans.CarBean">--> <!--<property name="name" value="保时捷"></property>--> <!--<property name="price" value="3000000"></property>--> <!--</bean>--> <bean id="car1" parent="car" p:name="奥迪"></bean> <bean id="person" class="com.spring.beans.PersonBean" depends-on="car" ></bean> </beans>
2(Bean的作用域)
•在 Spring 中, 可以在 <bean> 元素的 scope属性里设置 Bean 的作用域. •默认情况下, Spring 只为每个在 IOC 容器里声明的 Bean 创建唯一一个实例, 整个 IOC 容器范围内都能共享该实例:所有后续的 getBean() 调用和 Bean 引用都将返回这个唯一的Bean 实例.该作用域被称为 singleton, 它是所有 Bean 的默认作用域.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--spring使用bean来构造实例的时候默认使用的是单例作用域
使用scope来配置作用域
singleton:在整个容器的生命周期内只创建一个bean,是单例的,在容器初始化之前已经创建好了实例
prototype:原型的,容器初始化时不创建bean的实例,而在每次请求时都创建一个新的实例返回,也就是什么时候使用什么时候创建
request,session对应域对象,使用较少-->
<bean id="car" class="com.spring.beans.CarBean" scope="prototype">
<property name="name" value="保时捷"></property>
<property name="price" value="100000000"></property>
</bean>
<!--<bean id="car1" class="com.spring.beans.CarBean">-->
<!--<property name="name" value="保时捷"></property>-->
<!--<property name=
f997
"price" value="3000000"></property>-->
<!--</bean>-->
<bean id="car1" parent="car" p:name="奥迪"></bean>
<bean id="person" class="com.spring.beans.PersonBean" depends-on="car" ></bean>
</beans>
CarBean car1 =(CarBean) applicationContext.getBean("car"); CarBean car2 =(CarBean) applicationContext.getBean("car"); System.out.println(car1 == car2);
3(使用外部属性文件)
•在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和Bean 配置相分离•Spring 提供了一个 PropertyPlaceholderConfigurer的 BeanFactory后置处理器, 这个处理器允许用户将Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为${var}的变量, PropertyPlaceholderConfigurer从属性文件里加载属性, 并使用这些属性来替换变量.Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用
4(Bean的生命周期)
•Spring IOC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务. •Spring IOC 容器对 Bean 的生命周期进行管理的过程:–通过构造器或工厂方法创建 Bean 实例–为 Bean 的属性设置值和对其他 Bean 的引用–调用 Bean 的初始化方法–Bean 可以使用了–当容器关闭时, 调用 Bean 的销毁方法•在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法.•Bean 后置处理器允许在调用初始化方法前后对Bean 进行额外的处理.•Bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理, 而非单一实例. 其典型应用是: 检查 Bean 属性的正确性或根据特定的标准更改Bean 的属性.•对Bean 后置处理器而言, 需要实现
接口. 在初始化方法被调用前后, Spring 将把每个 Bean 实例分别传递给上述接口的以下两个方法:
•Spring IOC 容器对 Bean 的生命周期进行管理的过程:–通过构造器或工厂方法创建 Bean 实例–为 Bean 的属性设置值和对其他 Bean 的引用–将 Bean 实例传递给 Bean 后置处理器的 postProcessBeforeInitialization方法–调用 Bean 的初始化方法–将 Bean 实例传递给 Bean 后置处理器的 postProcessAfterInitialization方法–Bean 可以使用了–当容器关闭时, 调用 Bean 的销毁方法
package com.spring.beans; /** * Created by Administrator on 2018/4/2. */ public class BeanCycle{ private String name; public BeanCycle() { System.out.println("构造器"); } public String getName() { return name; } public void setName(String name) { this.name = name; System.out.println("初始化属性"); } public void init(){ System.out.println("init....."); } public void destroy(){ System.out.println("destroy....."); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="beanCycle" class="com.spring.beans.BeanCycle" init-method="init" destroy-method="destroy"> <property name="name" value="生命周期"></property> </bean> <!--配置bean的后置处理器--> <!--不需要配置id,Spring会自动识别--> <bean class="com.spring.beans.BeanProcessor"></bean> </beans>
package com.spring.beans; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * Created by Administrator on 2018/4/2. */ public class BeanProcessor implements BeanPostProcessor { //处理所有的bean,不单单处理一个,可以通过判断bean的id来进行指定的处理 //其中v参数:bean代表实例本身。 //beanName:IOC容器配置的bean的名字,也就是配置的bean的id //返回值:实际上返回给用户的bean (可以根据自己的需求修改bean,返回新修改的) @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { //init方法之前被调用 System.out.println("postProcessBeforeInitialization,"+ bean+ ","+beanName); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { //init方法之后被调用 System.out.println("postProcessAfterInitialization,"+ bean+ ","+beanName); //可以做其他业务处理 return bean; } }
相关文章推荐
- Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配
- Spring初学之bean之间的关系和bean的作用域
- spring学习笔记整理--04(配置Spring管理的bean的作用域、生命周期)
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- Spring -- Bean自动装配&Bean之间关系&Bean的作用域
- Spring -- Bean自己主动装配&Bean之间关系&Bean的作用域
- Spring4 学习笔记(6)-Spring 的 Bean 之间的关系
- Spring4之Bean之间的关系(继承、依赖、引用)Bean的作用范围
- spring知识三-----SpEL&生命周期&bean配置方式
- Spring 框架Bean之间的关系(继承、依赖)
- Spring BeanBean的作用域及生命周期
- Spring4的知识应用总结(四)——Bean的生命周期和注解方式配置
- Spring学习--Bean 之间的关系
- Spring中Bean的作用域、生命周期
- spring学习笔记四 bean之间的关系
- Spring学习笔记 6. 尚硅谷_佟刚_Spring_Bean 之间的关系
- spring知识二---(bean关系和作用域)
- spring学习历程---bean之间的关系
- spring学习小结——bean之间的关系 继承 依赖
- Spring记录之Bean属性配置、依赖关系及生命周期