Bean and beanfactory

Source: Internet
Author: User

Bean is the basic unit of spring management. In spring J2EE applications, all components are beans. Beans include data sources, sessionfactory of hibernate, and Transaction Manager. Bean in spring is a very broad concept. Any Java object or Java component can be treated as bean.

Beanfactory --> the most basic interfaces of spring containers. Beanfactory is responsible for configuring, creating and managing beans, and dependencies between beans.
The beanfactory Interface contains the following basic methods:
Boolean containsbean (string name ):
Determine whether the spring container contains the bean definition with the ID as name.
Object getbean (string name ):
Returns the bean with the container ID as name.
Object getbean (string name, class requiredtype ):
Return the bean whose ID is name and type is requiredtype in the container.
Class GetType (string name ):
Return the type of bean whose ID is name in the container.

Bean basic definition:
The following two attributes are required:
1 ID
2 class
Obviously, bean instantiation can be obtained through the getbean () method of beanfactory.
In spring1.2, bean has two basic types in spring containers:
1 singleton: single state
2not-singleton or prototype: Prototype
Every time the request ID is not-singleton type bean, spring will create a new bean instance and return it to the program. Generally, the Controller bean of the Web application must be configured as non-singleton type. Because each httpservletrequest operation requires the system to start a new action to process user requests. If no basic bean behavior is specified, spring uses the singleton type by default. The creation and destruction costs of non-singleton type beans are relatively high. After the singleton type bean instance is successfully created, we recommend that you avoid setting beans to the non-singleton type.

The following methods are generally used to create bean instances:
1. Call the constructor to create a bean instance.
Create a bean instance using the "new" keyword
2 beanfactory call the static factory method of a class to create Bean
When using a static factory to create a bean instance, the class attribute is also required, but the class attribute is not the Implementation class of the instance, but the static factory class. Spring needs to know which static factory method is used to create bean instances, so the factory-method attribute is used to determine the static factory method name.
3 beanfactory call the instance factory method to create Bean
The instance factory method must provide the factory instance, so the factory instance must be configured in the configuration file, and the bean element does not need the class attribute, because beanfactory no longer directly instantiates the bean, but only executes the factory method, generates bean instances.

The instance factory method and static factory method are similar in usage. Let's take a look at the differences and similarities between them:

1. When you call the instance factory method to create a bean, you must configure the instance factory as a bean instance, and the static factory rules do not need to configure the factory bean.
2. When you call the instance factory method to create a bean, you must use the factory-bean attribute to determine the factory bean, while the static factory method uses the class element to determine the static factory class.
1. You must use the factory-method attribute to specify the factory method for generating bean instances.
2. parameters are required for the factory method. The construtor-Arg attribute is used to determine the parameter value.
3. Other dependency injection attributes use the property element to determine the parameter value.

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: 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.