码迷,mamicode.com
首页 > 编程语言 > 详细

spring学习四

时间:2017-09-20 01:00:17      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:信息   nfa   st表   get   状态   tps   返回   spring实例   实例   

1: InitializingBean  vs  init-method

      InitializingBean 是一个接口,有一个方法afterPropertiesSet,不建议使用;因为InitializingBean是Spring接口,这样导致 bean和spring耦合到一起了。

     <bean id="SimpleBean1" class="com.SimpleBean" init-method="init" destroy-method="cleanUp >
        <property name="name" value="Bill"></property>
        <property name="age" value="19"></property>
      </bean> 

       DisposableBean接口 destroy() ,一般用destory-method替代

2:ApplicaitonContextAware ,   BeanNameAware接口, BeanFactoryAware 

      ApplicaitonContextAware :setApplicationContext()  可以让bean获取context信息。

      BeanNameAware:  可以让bean获得bean的name.

      BeanFactoryAware :  bean感知beanfactory接口

3: Spring中注入map的key必须是String类型。

4:bean的生命周期: 

  (1) Spring实例化bean.

  (2)  利用依赖注入来配置bean中所有的属性值。

  (3)  beanNameAware,   setBeanName

  (4)  BeanFactoryAware,   setBeanFacetory()

  (5)  ApplicationContextAware, setApplicationContext(), 当前ApplicationContext实例的引用。

  (6)  BeanPostProcessor,   posProcessBeforeINnitalzation()

  (7)  InitializingBean, posProcessAfterINitialization()

      到这为止,bean就可以被用了,

  (8) DisposableBean,  destroy()

  (9) destroy-method,  定义销毁bean的方法。

5: bean的作用域:

       scope = "singleton"(默认)

       prototype: 每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,相当于一个new的操作。

   request:request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,配置实例:    

<web-app>...<listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener>...</web-app> 
 
<bean id="role" class="spring.chapter2.maryGame.Role" scope="request"/> 

 

    session:  和request类似

   global session:

其中比较常用的是singleton和prototype两种作用域。对于singleton作用域的Bean,每次请求该Bean都将获得相同的实例。容器负责跟踪Bean实例的状态,负责维护Bean实例的生命周期行为;如果一个Bean被设置成prototype作用域,程序每次请求该id的Bean,Spring都会新建一个Bean实例,然后返回给程序。在这种情况下,Spring容器仅仅使用new 关键字创建Bean实例,一旦创建成功,容器不在跟踪实例,也不会维护Bean实例的状态。

 

 

spring学习四

标签:信息   nfa   st表   get   状态   tps   返回   spring实例   实例   

原文地址:http://www.cnblogs.com/liufei1983/p/7554590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!