Spring (04) Spring Bean foundation

Spring (04) Spring Bean foundation

Spring core programming ideas catalog: https://www.cnblogs.com/binarylei/p/12290153.html

1. Define Spring Bean

BeanDefinition Spring Framework is defined in the interface Bean meta configuration, comprising:

  • Bean class name
  • Bean behavioral configuration elements, such as scopes, automatic binding mode, lifecycle callbacks, etc.
  • Other references Bean, also be called collaborators (collaborators) or dependent (Dependencies)
  • Configuration settings, such as Bean properties (Properties)

2. BeanDefinition meta-information

Property (Property) Explanation
Class Bean full class name, must be a concrete class, not abstract classes or interfaces
Name Bean's name or ID
Scope Bean scopes (eg: singleton, prototype, etc.)
Constructor arguments Bean constructor parameter (dependency for injection)
Properties Bean property (for dependency injection)
Autowiring mode Bean automatic binding mode (eg: by name byName)
Lazy initialization mode Bean lazy initialization mode (non-delayed and delayed)
Initialization method Bean initialization callback method name
Destruction method Bean destroy callback method names

Construction of BeanDefinition

  • BeanDefinitionBuilder
  • AbstractBeanDefinition and derived classes

3. Name the Spring Bean

3.1 Bean name

Each Bean has one or more identifiers (identifiers), container these identifiers Bean is located must be unique. Typically, a Bean is only one identifier, if additional, consider using an alias (Alias) to expand.

In XML-based configuration meta-information, developers can use id or name attribute to specify Bean identifier. Bean is usually identified by letters, special characters are allowed. To introduce an alias if the Bean, it can be used in the name attribute comma ( ",") or semicolons ( ';') are spaced.

Bean's id or name attribute does not need to develop, if left blank, the container will automatically generate a unique name for Bean. Bean naming Although there is no limit, but the official suggested hump manner more consistent with Java naming conventions.

3.2 Bean name generator (BeanNameGenerator)

Introduced by the Spring Framework 2.0.3, two implementations built frame:

  • DefaultBeanNameGenerator: BeanNameGenerator achieve universal default
  • AnnotationBeanNameGenerator: annotation-based scanning BeanNameGenerator achieve, the official beginning of the document to the Spring Framework 2.5, associated with:

4. Spring Bean alias

Value Bean Alias ​​(Alias) of

  • Reuse existing BeanDefinition
  • More naming of a scene, such as:
<alias name="myApp-dataSource" alias="subsystemA-dataSource"/>
<alias name="myApp-dataSource" alias="subsystemB-dataSource"/>

5. Registration Spring Bean

5.1 BeanDefinition registration

  • XML configuration meta-information

    <bean name=”...” />
  • Java annotations configuration meta-information

    • @Bean
    • @Component
    • @Import
  • Java API configuration meta-information

    • naming method:

      BeanDefinitionRegistry#registerBeanDefinition(String, BeanDefinition)
    • Non-naming:

      BeanDefinitionReaderUtils#registerWithGeneratedName(AbstractBeanDefinition, BeanDefinitionRegistry)
    • Class configuration mode:

      AnnotatedBeanDefinitionReader#register(Class...)

5.2 External singleton object registration

  • Java API configuration meta-information

    SingletonBeanRegistry#registerSingleton

6. Spring Bean instantiation

Bean instantiation (Instantiation)

  • Conventional way
    • By constructor (configuration meta-information: XML, Java annotations and Java API)
    • By static factory method (configuration meta-information: XML and Java API)
    • By Bean factory method (configuration meta-information: XML and Java API)
    • By FactoryBean (configuration meta-information: XML, Java annotations and Java API)
  • Special way
    • By ServiceLoaderFactoryBean (configuration meta-information: XML, Java annotations and Java API)
    • 通过 AutowireCapableBeanFactory#createBean(java.lang.Class, int, boolean)
    • 通过 BeanDefinitionRegistry#registerBeanDefinition(String,BeanDefinition)

7. initialization Spring Bean

Bean Initialization (Initialization)

  • @PostConstruct Tagging
  • InitializingBean interface implemented afterPropertiesSet () for
  • Custom initialization method
    • XML 配置:<bean init-method=”init” ... />
    • Java annotations: @Bean (initMethod = "init")
    • Java API:AbstractBeanDefinition#setInitMethodName(String)

Thoughts: Assuming the above three methods are defined in the same Bean, then the order of execution of these methods is how?

8. Delay Initialization Spring Bean

Bean lazy initialization (Lazy Initialization)

  • XML configuration: <bean lazy-init = "true" ... />
  • Java annotations: @Lazy (true)

Question: When is lazy initialization, then, there are differences in how the object returned by the Spring container objects of a non-delayed Bean defined?

9. Destruction Spring Bean

Bean destroy (Destroy)

  • @PreDestroy Tagging
  • DisposableBean interface implemented destroy () method
  • Custom destruction method
    • XML 配置:<bean destroy=”destroy” ... />
    • Java 注解:@Bean(destroy=”destroy”)
    • Java API:AbstractBeanDefinition#setDestroyMethodName(String)

Thoughts: Assuming the above three methods are defined in the same Bean, then the order of execution of these methods is how?

10. The garbage collection Spring Bean

Bean garbage collection (GC)

  • Close Spring container (application context)
  • GC execution
  • Spring Bean covered finalize () method is called back

11. The selection of interview questions

Question 1: How to register a Spring Bean?

A: By registering monomer BeanDefinition and external objects.

Question 2: What is the Spring BeanDefinition?

A: Review of "Defining Spring Bean" and "BeanDefinition meta-information."

Question 3: Spring container is how to manage registered Bean?

A: The answer will be discussed in more detail in subsequent sections of topics, such as: IoC configuration meta-information read and parse, find and rely on the injection and Bean life cycle.


The intentions of recording a little bit every day. Perhaps the content is not important, but the habit is very important!

Guess you like

Origin www.cnblogs.com/binarylei/p/12296917.html
Recommended