7--Basic usage of spring--5 ...

Source: Internet
Author: User
Tags xml parser

7.5 Beans in the spring container

Basic definitions and bean aliases for 7.5.1 beans

The <beans.../> element is the root element of the spring configuration file, which can specify the following properties:

Default-lazy-init: Specifies the default deferred initialization behavior for all beans configured under the <beans.../> element.

Default-merge: Specifies the default merge behavior for all beans configured under the <beans.../> element.

Default-autowire: Specifies the default automatic assembly behavior for all beans configured under the <beans.../> element.

Default-autowire-candidates:: Specifies whether all the beans configured under the <beans.../> element are default as candidate beans for automatic assembly.

Default-init-method: Specifies the default initialization method for all beans configured under the <beans.../> element.

Default-destroy-method: Specifies the default collection method for all beans configured under the <beans.../> element.

The properties that can be specified under the <beans.../> element can be specified in each <bean.../> child element----The attribute name is removed from the default. The difference is that these properties, specified for <bean.../>, work only on specific beans, and if you specify these properties under the <beans.../> element, these properties will work for all the beans that <beans.../> contains. When the properties specified by the two are inconsistent, the properties specified under,<bean.../> Override the properties specified under <beans.../>.

The id attribute of the <bean.../> element is unique and is a true XML ID attribute, so other XML elements can take advantage of the XML parser's validation capabilities when referencing the ID.

There are two ways of specifying aliases:

⊙ specify an alias by the Name property when defining the <bean.../> element, or if you need to specify multiple aliases for a bean instance, you can use commas, colons, or spaces in the Name property to separate multiple aliases, and then access the bean instance by any alias.

⊙ specifies an alias for an existing bean through the <alias.../> element.

<alias name= "" alias= ""/> Name: Specifies the identity name of a bean instance that specifies an alias for the Bean instance; alias: Specifies an alias.

<BeanID= "Person"class= "Edu.pri.lime._7_4_5.bean." Person "name= "Chinese,lime,oracle"/>    <aliasname= "Person"alias= "Jackson"/>    <aliasname= "Person"alias= "Tomcat"/>

Scope of Bean in 7.5.2 container

When you create a bean instance from a spring container, you can not only instantiate the bean instance, but also specify a specific scope for the bean.

⊙singleton: Singleton mode, in the entire spring IOC container, the singleton-scoped Bean will generate only one instance.

⊙prototype: Each time a bean of the prototype scope is obtained through the container's Getbean () method, a new bean instance is generated.

⊙request: For an HTTP request, the request scoped Bean will generate only one instance, which means that within the same HTTP request, every time the program requests the bean, the same instance is always obtained. This scope is only really valid if you are using spring in a web app.

⊙session: For an HTTP reply, the session-scoped bean will generate only one instance, which means that the same instance is always obtained for each time the program requests the bean during the same HTTP call. This scope is only really valid if you are using spring in a web app.

⊙global session: Each global HTTP session corresponds to a bean instance. In a typical case, only valid when using the Portlet context. This scope is only really valid if you are using spring in a web app.

More commonly used are singleton and prototype two scopes, and for singleton scoped beans, each request for the bean will get the same instance. The container is responsible for keeping track of the state of the bean instance and is responsible for maintaining the life cycle behavior of the bean instance; If a bean is set to prototype scope, the program will create a new bean instance each time the Bean,spring requests that ID, and then return to the program. In this case, the spring container only uses the new keyword to create the bean instance, and once it is successfully created, the container no longer tracks the instance and does not maintain the state of the bean instance.

If you do not specify a scope for the bean, Spring uses the singleton scope by default.

Java requires a memory request when the Java instance is created, and garbage collection is required to destroy the instance, which can lead to increased system overhead. Therefore, the prototype scope of the bean creation, destruction cost is relatively large. Once the bean instance of the singleton scope is successfully created, it can be reused.

The spring configuration file specifies the scope of the bean through the scope property.

7--Basic usage of spring--5 ...

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.