Spring总结(二)--Bean的探究

来源:互联网 发布:女子不孕不育网络咨询 编辑:程序博客网 时间:2024/05/26 08:43

探究一:从ApplicationContex 应用上下文容器中获取bean和从bean工厂容器中获取bean。

首先建立Student的pojo类,并初始化构造器。

  1. package com.spring.ioc;
  2. publicclassStudent{
  3. privateString name;
  4. publicStudent(){
  5. System.out.println("student被初始化了");
  6. }
  7. publicString getName(){
  8. return name;
  9. }
  10. publicvoid setName(String name){
  11. this.name = name;
  12. }
  13. }
写beans.xml文件
  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 http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="student"class="com.spring.ioc.Student">
  6. <property name="name" value="nihao"></property>
  7. </bean>
  8. </beans>

第一种方式:

ApplicationCotext,spring更加高级的容器。功能强大:

1.提供文本信息解析工具,包括对国际化支持。

2.提供载入文件资源的通用方法,如图片。

3.可以向注册为监听器的bean发送事件。


  1. package com.spring.ioc;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. publicclassTest{
  5. publicstaticvoid main(String[] args){
  6. ApplicationContext ac=newClassPathXmlApplicationContext("classpath:com/spring/ioc/beans.xml");
  7. Student student=(Student)ac.getBean("student");
  8. System.out.println(student.getName());
  9. }
  10. }
若只加载这一行,ApplicationContext ac=newClassPathXmlApplicationContext("classpath:com/spring/ioc/beans.xml");运行结果发现student类的构造器的方法打印出来,
这个方法就说明已经加载类了。

第二种方式:
  1. BeanFactory factory =newXmlBeanFactory(
  2. newClassPathResource("com/spring/ioc/beans.xml"));
  3. //如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么

    //容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.

运行结果:
  1. 三月07,20179:42:08下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  2. 信息: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

  1. <beanid="student"class="com.spring.ioc.Student"scope="singleton">
  2. <propertyname="name"value="nihao"></property>
  3. </bean>
  1. public static void main(String[] args) {
  2. ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:com/spring/ioc/beans.xml");
  3. Student student1=(Student)ac.getBean("student");
  4. Student student2=(Student)ac.getBean("student");
  5. System.out.println(student1);
  6. System.out.println(student1);
运行结果:

  1. 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@9807454]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3b22cdd0
  2. 三月 07, 2017 9:48:04 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  3. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3b22cdd0: defining beans [student]; root of factory hierarchy
  4. student被初始化了
  5. com.spring.ioc.Student@5b1d2887
  6. com.spring.ioc.Student@5b1d2887
由运行结果可以看出当bean的scope范围设置singleton后,对象实例化的时候只占用一块内存空间,产生的对象也是相同的。

2.prototype
运行结果:
  1. 三月 07, 2017 9:54:33 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  2. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3b22cdd0: defining beans [student]; root of factory hierarchy
  3. student被初始化了
  4. student被初始化了
  5. com.spring.ioc.Student@66133adc
  6. com.spring.ioc.Student@66133adc

尽量使用 scope=singleton ,不要使用prototype,因为这样对我们的性能影响较大。


探究三:bean的生命周期

 

 实例化(当我们的程序加载beans.xml文件),把我们的bean(前提是scope=singleton)实例化到内存

 调用set方法设置属性

 如果你实现了bean名字关注接口(BeanNameAware) 则,可以通过setBeanName获取id

 如果你实现了 bean工厂关注接口,(BeanFactoryAware),则可以获取BeanFactory

 如果你实现了 ApplicationContextAware接口,则调用方法

  1. //该方法传递ApplicationContext
  2. publicvoid setApplicationContext(ApplicationContext arg0)
  3. throwsBeansException{
  4. // TODO Auto-generated method stub
  5. System.out.println("setApplicationContext"+arg0);
  6. }

 如果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

 



0 0
原创粉丝点击