Spring Bean Definition

Source: Internet
Author: User
Tags aliases

In the previous example of user registration, we have used spring to define a user persistence class:

<bean id= "Userdao" class= "Com.dev.spring.simple.MemoryUserDao"/>

This is one of the simplest bean definitions. It is similar to calling a statement: Memoryuserdaouserdao = new Memoryuserdao ().

The ID attribute must be a valid xmlid, which means it must be unique throughout the XML document. It is a bean's "Lifetime Code (9527)". You can also use the Name property to define one or more individual names for the bean (separate multiple aliases with commas or spaces). The Name property allows arbitrary illegal XML letters to appear. For example:

<bean id= "Userdao" Name= "Userdao*_1, userdao*_2"

class= "Com.dev.spring.simple.MemoryUserDao"/>.

The class attribute defines the fully qualified class name (package name + class name) for this bean. Spring can manage almost all Java classes. Generally, this Java class will have a default constructor that sets the dependent properties with the set method.

The bean element is out of the two attributes above, and there are many other attributes. The description is as follows:

<bean

Id= "Beanid" (1)

Name= "Beanname" (2)

Class= "Beanclass" (3)

Parent= "Parentbean" (4)

Abstract= "true | False "(5)

Singleton= "true | False "(6)

Lazy-init= "true | False | Default "(7)

ByName | Bytype | constructor | AutoDetect |default "(8)

Dependency-check = "None | Objects | Simple | All |default "(9)

Depends-on= "Dependsonbean" (10)

Init-method= "Method" (11)

Destroy-method= "Method" (12)

Factory-method= "Method" (13)

factory-bean= "Bean" > (14)

</bean>

(1) The unique identification name of the Id:bean. It must be legal xmlid, the only one in the entire XML document.

(2), Name: Used to create one or more individual names for IDs. It can be any letter that fits. Multiple aliases are separated by commas or spaces.

(3), class: To define the fully qualified name of the class (package name + class name). Only the subclass Bean does not define the attribute.

(4), Parent: The subclass Bean defines the parent class bean that it references. The previous class attribute is invalidated. The subclass Bean inherits all the properties of the parent class bean, and the subclass Bean can overwrite the properties of the parent bean. Note: The subclass Bean and the parent class bean are the same Java class.

(5), abstract (default is "false"): used to define whether the bean is an abstract bean. It means that the bean will not be instantiated and is typically used by the parent class Bean, because the parent class bean is primarily used by the subclass Bean inheritance.

(6), Singleton (default is "true"): Defines whether the Bean is singleton (a single case). If set to True, only one instance of this bean is maintained within beanfactory scope. If set to "Flase", the bean will be a prototype (prototype) state, and Beanfactory will create a new bean instance for each bean request.

(7), Lazy-init (default "defaults"): Used to define whether the bean implements lazy initialization. If "true", it initializes all Singletonbean at beanfactory startup. Conversely, if "false," it starts creating Singletonbean only when the bean is requested.

(8), Autowire (automatic assembly, defaults to "default"): It defines how the bean is automatically loaded.

1, "No": do not use the automatic assembly function.

2, "byname": The Bean's property name to achieve automatic assembly.

3, "Bytype": the type of bean to achieve automatic assembly.

4, "constructor": similar to the Bytype, but it is used for the constructor function of the automatic assembly of parameters.

5, "AutoDetect": Through the Bean class introspection mechanism (introspection) decide whether to use "constructor" or "Bytype".

(9), Dependency-check (dependency check, defaults to "default"): It is used to ensure that the bean component is described by JavaBean so that dependencies are met. It is particularly useful when used with automatic assembly features.

1, none: no dependency checking.

2, objects: only to do the inspection of the dependencies between objects.

3. Simple: Check only for original type and string type dependencies

4. All: Check all types of dependencies. It includes the front objects and simple.

(10), depends-on (Dependent object): The object that this bean relies on when it is initialized, which is created before the bean is initialized.

(11), Init-method: The initialization method used to define the bean, which is invoked after the bean is assembled. It must be a method with no parameters.

(12), Destroy-method: Used to define the method of destroying a bean, which is invoked when Beanfactory closes. Again, it must be a method without parameters. It can only be applied to Singletonbean.

(13), Factory-method: Defines the factory method that creates the Bean object. It is used for the following "Factory-bean", which means that the bean is created from the factory method. At this point, the "class" property is invalidated.

(14), Factory-bean: Defines the factory class that created the Bean object. If "Factory-bean" is used, the "class" property is invalidated.

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.