Spring Source parsing-beanfactory

Source: Internet
Author: User
Tags aliases ldap

Beanfactory is the core interface for spring to implement dependency injection. Provides the unified configuration registration function of the application to realize the business development of the solution. Using Getbean can replace the singleton, prototype design pattern.

The notes in the top beanfactory are very well written. So let's translate the notes first, and then analyze them in detail later.

Focus directly on the red label.

The root interface for accessing a Spring bean container.
This is the basic client view of a bean container;
Further interfaces such as {@link listablebeanfactory} and
{@link Org.springframework.beans.factory.config.ConfigurableBeanFactory}
is available for specific purposes.

Accesses the root interface of a Spring bean container . This is a basic client view of a bean container; further interfaces, such as {@link listablebeanfactory} and {@link org.springframework.beans.factory.config.ConfigurableBeanFactory} can be used for special purposes.

This interface was implemented by objects a number of bean definitions,
Each uniquely identified by a String name. Depending on the bean definition,
The factory would return either an independent instance of a contained object
(the Prototype design pattern), or a single shared instance (a superior
alternative to the Singleton design pattern, in which the instance is a
Singleton in the scope of the factory). Which type of instance would be returned
depends on the beans factory configuration:the API is the same. Since Spring
2.0, further scopes is available depending on the concrete application
context (e.g. "Request" and "session" scopes in a Web environment).

this interface is implemented by objects that hold some bean definitions, and each bean is uniquely identified by a string string. Based on the bean definition,The factory will return a standalone object instance (prototype design pattern), or a single shared instance (an elegant alternative to the singleton design pattern, where the instance is a singleton within a factory range). Which type of instance will be returned depends on the Bean factory configuration: Even if the API is the same. Starting with Spring2.0, scopes extend to specific application contexts, such as the request,session of a Web environment.

The point of this approach is and the Beanfactory is a central registry
of application components, and centralizes configuration of application
Components (no more does individual objects need to read properties files,
For example). See chapters 4 and one of "Expert one-on-one-ee Design and
Development "for a discussion of the benefits of this approach.

The key to this scenario is that Beanfactory is the hub for application component registration, while centralizing the configuration of application components (program modules no longer need to read configuration files such as properties). More benefits of this design are discussed in the <J2EE Design Development Programming Guide > chapters 4th and 11th.

Strongly recommend to read this book, is not good at home to buy.

Note that it's generally better to rely on Dependency injection
("Push" configuration) to configure application objects through setters
or constructors, rather than use any form of ' pull ' configuration like a
Beanfactory Lookup. Spring ' s Dependency injection functionality is
Implemented using this Beanfactory interface and its subinterfaces.

Compared to the pull configuration found in beanfactory, it is better to configure the application object by means of a setters or construction method, depending on the injection. Spring's dependency injection function is implemented by implementing Beanfactory and its sub-interfaces .

normally a beanfactory would load bean definitions stored in a configuration
source (such as an XML document), and use the {@code Org.springframework.beans}
Package to Configure the beans. However, an implementation could simply return
Java objects It creates as necessary directly in Java code. There is no
Constraints on how the definitions could is Stored:ldap, RDBMS, XML,
properties file, etc. Implementations is encouraged to support references
amongst beans (Dependency injection).

Typically, a beanfactory is configured from a configuration source such as x?? ml file) to load the Bena definition and parse the bean using {@code Org.springframework.beans} package. However, implementations can simply return Java code to a new Java object directly. There is no restriction on the format of the bean definition file: Ldap,rdbms,xml. Implementation class welcome support app instead of bean (dependency injection)


In contrast to the methods in {@link listablebeanfactory}, all of the
Operations in this interface would also check parent factories if this is a
{@link hierarchicalbeanfactory}. If A bean is not found in this factory instance,
The immediate parent factory would be asked. Beans in this factory instance
is supposed to override beans of the same name in any parent factory.

In contrast to the method in {@link listablebeanfactory}, if this is a {@link hierarchicalbeanfactory}, all implementations of this interface will look for the parent factory . If the bean is not found in this factory instance, go directly to the parent factory to find it. The Bean in the factory instance overwrites the bean in the parent factory instance with the same name.

Bean factory implementations should support, the standard bean lifecycle interfaces
As far as possible. The full set of initialization methods and their-order is:
The Bean Factory implementation class should support the life-cycle interface of the standard bean as much as possible. Full set of initialization methods, sorted as follows

It feels like a good idea.
1. Beannameaware ' s {@code setbeanname}
2. Beanclassloaderaware ' s {@code setbeanclassloader}
3. Beanfactoryaware ' s {@code setbeanfactory}
4. Resourceloaderaware ' s {@code setresourceloader}
(only applicable while running in an application context)
5. Applicationeventpublisheraware ' s {@code setapplicationeventpublisher}
(only applicable while running in an application context)
6. Messagesourceaware ' s {@code Setmessagesource}
(only applicable while running in an application context)
7. Applicationcontextaware ' s {@code setapplicationcontext}
(only applicable while running in an application context)
8. Servletcontextaware ' s {@code setservletcontext}
(only applicable if running in a Web application context)
9. {@code Postprocessbeforeinitialization} methods of Beanpostprocessors
Initializingbean ' s {@code afterpropertiesset}
One . A custom Init-method definition
{@code Postprocessafterinitialization} methods of Beanpostprocessors

On shutdown of a bean factory, the following lifecycle methods apply:
1. Disposablebean ' s {@code destroy}
2. A custom Destroy-method definition

1  Packageorg.springframework.beans.factory;2  Public InterfaceBeanfactory {3 4     /**5 * Used to distinguish whether to get Factorybean instances directly.6 * Bean starts with & to get the Factorybean instance. Otherwise get an instance of created. For example, if the bean named7      * {@codeMyjndiobject} is a factorybean, getting {@code&myjndiobject}8 * would return the factory, not the instance returned by the factory.9      */TenString Factory_bean_prefix = "&"; One  A     /** - * Returns a prototype or singleton instance. - * Grab a single case, prototype design mode of the rice bowl the * Can be found based on aliases, or you can go to the parent container instance to find -      */ -Object Getbean (String name)throwsbeansexception; -  +     /** - * Add a type +      */ A<T> T Getbean (String name, class<t> requiredtype)throwsbeansexception; at  -     /** - * Gets the bean instance based on the type. can be an interface or subclass, but not {@codenull}. -      * {@linkListablebeanfactory} You can also use type conversion to name to find it. More operations on bean collections can be seen - * Listablebeanfactory and Beanfactoryutils -      */ in<T> T Getbean (class<t> requiredtype)throwsbeansexception; -  to     /** + * More construction methods, factory method parameters -      */ theObject Getbean (String name, Object ... args)throwsbeansexception; *  $     /**Panax Notoginseng * Determine whether to include beans (including aliases, parent containers) - * Traps appear: this way regardless of whether the class is abstract class, lazy loading, whether within the scope of the container, as long as the compliance all return true, so this side true, not necessarily can get the instance from Getbean the      */ +     BooleanContainsbean (String name); A  the     /** + * is a single case -      */ $     BooleanIssingleton (String name)throwsnosuchbeandefinitionexception; $  -     /** - * Whether the prototype the      */ -     BooleanIsprototype (String name)throwsnosuchbeandefinitionexception;Wuyi  the     /** - * If there is a bean with the name match type Wu      */ -     BooleanIstypematch (String name, class<?> targetType)throwsnosuchbeandefinitionexception; About  $     /** - * Gets the type according to the bean name -      */ -Class<?> GetType (String name)throwsnosuchbeandefinitionexception; A  +     /** the * Get aliases -      */ $ string[] getaliases (String name); the  the}

Spring Source parsing-beanfactory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.