Spring lecture notes (topic II)

 

 

 

 

Chapter 3 Spring Bean assembly (on)

3-1: configuration items and scope

  

  1.Bean configuration items:

    -- Id

    - Class (this must be, the other can not configure)

    - Scope (Scope)

    - Constructor arguments (attributes injection: constructor injection)

    - properties (attributes injection: set value of the injected)

    - Autowired mode (automatic assembly mode)

    - lazy-initialization mode (mode lazy loading)

    - Initialization / destruction method (initialization and destruction)

   2. Obtaining

    - byName (bean requirements have to be acquired id)

    - byType (bean is required to have is class, id optional)

   3. bean scopes

    - singleton: single embodiment, there is only a one vessel Bean (default)

    - prototype: every request (each time) to create a new instance, destroy the way does not take effect

    - request: every http request to create an instance and only valid for the current request

    - session: ibid., Every http request to create, within the current session valid

    - global session: a valid based web portletde (the portlet defines global session), if in the web, the same session (for multiple subsystems)

      Such as: the use of a singleton, so only one container Bean

      

 

3-2 Bean's life cycle:

  1. Life Cycle: Definitions -> Initialization -> Use -> destruction

  2. initialized in two ways:

    - implement the org.springframework.beans.factory.InitializingBean covering method afterPropertiesSet

      

    - XML ​​configuration init-method:

      

   3. The destruction of the two methods

     - implement org.springframework.beans.factory.DisposableBeans interface cover destroy method

        

     - Configure destroy-method

      

    4. Global default initialization, destruction methods

      

   5. summarized in three configurations initialization methods of destruction

          (Interface> Profiles)> Global

   Implementation of the interface initialization and destruction before the initialization and destruction methods configured in the configuration file, they will override the global configuration of these methods, the method does not perform global configuration. Configuring global initialization and destruction methods, even without implementing these methods, it does not complain. The other two methods must be configured to achieve, otherwise it will error

 

3-3 Aware Interface

  1 Overview

    - Spring provides a number ending in Aware's interface, the bean Aware interface after being initialized, you can get the appropriate resources

    -, can be operated by a respective resource Spring Aware interface (must be careful)

    - provides a convenient entry point for a simple extension of the Spring

  2. Common Interface

    

    

    

    For example: ApplicationContextAware: Bean class implements this interface, this interface provided by the method, you can get spring context directly, without having to manually create our own, it can also be used to get a context to judge whether there is some bean

    xml:

      

    Java:

      

    JUnit

      

    result

      

 

3-4 Bean autowire

  0.5 Overview

    

   1). Object is fitted

    

   2). Assembling object

    

  3). Xml configuration

    

   4) .JUnit test

    

   5) Results

    

 

 

 

 

 

 

 

      

 

 

    

 

  

 

 

 

 

 

 

 

  

 

 

  

  

 

  

Guess you like

Origin www.cnblogs.com/heyboom/p/11232644.html