Spring总结(二)--Bean的探究
来源:互联网 发布:女子不孕不育网络咨询 编辑:程序博客网 时间:2024/05/26 08:43
探究一:从ApplicationContex 应用上下文容器中获取bean和从bean工厂容器中获取bean。
首先建立Student的pojo类,并初始化构造器。
package com.spring.ioc;
publicclassStudent{
privateString name;
publicStudent(){
System.out.println("student被初始化了");
}
publicString getName(){
return name;
}
publicvoid setName(String name){
this.name = name;
}
}
<?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.xsd">
<bean id="student"class="com.spring.ioc.Student">
<property name="name" value="nihao"></property>
</bean>
</beans>
第一种方式:
ApplicationCotext,spring更加高级的容器。功能强大:
1.提供文本信息解析工具,包括对国际化支持。
2.提供载入文件资源的通用方法,如图片。
3.可以向注册为监听器的bean发送事件。
package com.spring.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
publicclassTest{
publicstaticvoid main(String[] args){
ApplicationContext ac=newClassPathXmlApplicationContext("classpath:com/spring/ioc/beans.xml");
Student student=(Student)ac.getBean("student");
System.out.println(student.getName());
}
}
BeanFactory factory =newXmlBeanFactory(
newClassPathResource("com/spring/ioc/beans.xml"));
//如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么
//容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.
三月07,20179:42:08下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息:Loading XML bean definitions from class path resource [com/spring/ioc/beans.xml]
结论:
1.如果使用ApplicationContext,则配置的bean如果是singlton不管你用不用,都被实例化.(好处就是可以预先加载,缺点就是耗内存)
2.如果是BeanFactory ,则当你获取beanfacotry时候,配置的bean不会被马上实例化,当你使用的时候,才被实例(好处节约内存,缺点就是速度)
3.规定:一般没有特殊要求,应当使用ApplicatioContext完成(90%)
探究二:bean的作用域
Spring Framework支持五种作用域(其中有三种只能用在基于web的)。
singleton
在每个Spring IoC容器中一个bean定义对应一个对象实例。
prototype
一个bean定义对应多个对象实例。
request
在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。
session
在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。
global session
在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的SpringApplicationContext情形下有效。
1.Singleton
<beanid="student"class="com.spring.ioc.Student"scope="singleton">
<propertyname="name"value="nihao"></property>
</bean>
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:com/spring/ioc/beans.xml");
Student student1=(Student)ac.getBean("student");
Student student2=(Student)ac.getBean("student");
System.out.println(student1);
System.out.println(student1);
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@9807454]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3b22cdd0
三月 07, 2017 9:48:04 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3b22cdd0: defining beans [student]; root of factory hierarchy
student被初始化了
com.spring.ioc.Student@5b1d2887
com.spring.ioc.Student@5b1d2887
三月 07, 2017 9:54:33 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3b22cdd0: defining beans [student]; root of factory hierarchy
student被初始化了
student被初始化了
com.spring.ioc.Student@66133adc
com.spring.ioc.Student@66133adc
尽量使用 scope=”singleton” ,不要使用prototype,因为这样对我们的性能影响较大。
探究三:bean的生命周期
① 实例化(当我们的程序加载beans.xml文件),把我们的bean(前提是scope=singleton)实例化到内存
② 调用set方法设置属性
③ 如果你实现了bean名字关注接口(BeanNameAware) 则,可以通过setBeanName获取id号
④ 如果你实现了 bean工厂关注接口,(BeanFactoryAware),则可以获取BeanFactory
⑤ 如果你实现了 ApplicationContextAware接口,则调用方法
//该方法传递ApplicationContext
publicvoid setApplicationContext(ApplicationContext arg0)
throwsBeansException{
// TODO Auto-generated method stub
System.out.println("setApplicationContext"+arg0);
}
⑥ 如果bean 和 一个后置处理器关联,则会自动去调用 Object postProcessBeforeInitialization方法
⑦ 如果你实现InitializingBean 接口,则会调用 afterPropertiesSet
⑧ 如果自己在<bean init-method=”init” /> 则可以在bean定义自己的初始化方法.
⑨ 如果bean 和 一个后置处理器关联,则会自动去调用 Object postProcessAfterInitialization方法
⑩ 使用我们的bean
11. 容器关闭
12. 可以通过实现DisposableBean 接口来调用方法 destory
13. 可以在<bean destory-method=”fun1”/> 调用定制的销毁方法
小结: 我们实际开发中往往,没有用的这么的过程,常见的是:
1->2->6->10->9->11
- Spring总结(二)--Bean的探究
- Spring之Bean总结(二)
- spring(二)---bean的学习下篇
- Spring(二)——Spring管理Bean的原理
- spring bean的使用总结
- Spring(二)--Bean装配
- Spring之Bean(二)
- Spring Bean载入(二)
- Spring 学习(二) Bean
- Spring(二)---装配Bean
- spring入门(4)---spring中Bean的生命周期总结
- Spring探究(2)——Bean和AOP
- spring学习总结(五)---Spring基础知识(二:Bean的配置项及其作用域)
- Spring的核心容器Bean的基础知识(二)
- 浅谈spring——Bean的生命周期(二)
- Spring学习笔记(二)Bean的作用域
- Spring学习(二十)Bean 的依赖关系介绍
- spring源码阅读之Bean的加载(二)
- Ros创建工作空间
- lintcode Find the Missing Number II
- CentOS7.0安装Apache服务器httpd
- android studio 使用中的一些错误总结
- css
- Spring总结(二)--Bean的探究
- angular $http请求,spring mvc如何获得参数
- react-native-easy-toast, 一款简单易用的 Toast 组件,支持 Android&iOS.
- ACM程序设计书中题目--J(大写字母的更替)
- sdutacm-数据结构实验之数组二:稀疏矩阵
- 练习
- UTf-8 和 UTF-16 区别
- 记一次mysql连接数超200线上故障
- 深度学习21天实战Caffe学习笔记--笔记5--caffe中前向传播、反向传播及最优化求解过程