Sping Memo 3 (covering spring2.5)

Source: Internet
Author: User

Bean Scope

Scope

Description

Singleton

In each Spring IoC container, a bean definition corresponds to an object instance.

Prototype

A bean defines multiple object instances.

Request

In an HTTP request, a bean corresponds to an instance, that is, each HTTP request will have its own bean instance, which is created based on a bean definition. This scope is only valid in the case of Web-based Spring applicationcontext.

Session

In an http session, a bean defines an instance. This scope is only valid in the case of Web-based Spring applicationcontext.

Global session

In a global http session, a bean defines an instance. In typical cases, it is valid only when the Portlet context is used. This scope is only valid in the case of Web-based Spring applicationcontext.

A) Singleton (default)

When the scope of a bean is Singleton, only one shared bean instance exists in the Spring IoC container, and all requests to the bean, as long as the ID matches the bean definition, only the same bean instance is returned. When a bean definition is set to the singlton scope, the Spring IoC container will only create the bean DefinitionUniqueInstance. This single instance will be stored in the singleton cache andAll subsequent requests and references for this beanWill return the cached object instance.

Set the attribute of the specified scope to sigleton:

<Bean id = "userdao" class = "com. Kay. Dao. impl. userdaoimpl" Scope = "Singleton"> </bean>

B) Prototype

The bean in the prototype scope will cause every request to this bean (inject it into another bean, orProgramWhen you call the getbean () method of the container, a new bean instance is created. Based on experience, prototype scope should be used for stateful beans, while Singleton scope should be used for stateless beans.

Set the specified scope attribute to prototype:

<Bean id = "userdao" class = "com. Kay. Dao. impl. userdaoimpl" Scope = "prototype"> </bean>

C) Other scopes (request session globalsession)

For more information, see the spring development manual.

Delayed bean Loading

The default format of applicationcontex is to load and instantiate all Singleton beans during initialization. If you do not need to instantiate a singleton bean during initialization during development, you need to use the lazy-init attribute in the configuration file for configuration:

<Bean id = "userbean" class = "com. Kay. Bean. userbean" lazy-init = "true"/>

Note: If a non-delayed loading Singleton bean depends on a delayed loading Singleton bean, the delayed loading Singleton bean will be instantiated when the non-delayed loading Singleton bean is instantiated, that is to say, the delayed loading function will become invalid.

Scan beans in classpath in spring2.5

If bean is configured in the spring configuration file, the size of the configuration file will increase rapidly as the project progresses, increasing the burden on developers to maintain the configuration file. In spring2.5, the spring container provides the function of automatically scanning beans in classpath using annotation annotations. This function can greatly reduce the volume of configuration files.

Procedure:

1. Import the common-annotations.jar package (this package is in the LIB/J2EE/of the spring2.5 full package)

2. Modify the schema of the spring configuration file as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<Beans xmlns = "http://www.springframework.org/schema/beans"

Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"

Xmlns: context = "http://www.springframework.org/schema/context"

Xsi: schemalocation = "http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-2.5.xsd>

</Beans>

3. Add the following configuration to the configuration file:

<Context: component-scan base-package = "com. Kay"/>

The base-package attribute specifies the package to be scanned. The spring container automatically scans the package and its sub-packages.

4. Add the following four types of annotation to the bean to be managed:

1) @ service à business logic Bean (such as service)

2) @ controller à view Bean (for example, Struts action)

3) @ component à unclassified Bean (for example, cut bean)

4) @ repository à instantiate Bean (such as Dao)

In the current spring version (2.5.6), there is no difference in processing the above four types of Annotation. Therefore, you only need to add the above random annotation to the bean to be managed.

For example:

@ Component

Public ClassUserbean {

Public VoidAdd ()

{

System.Out. Println ("add method is runing ~~~~~ ");

}

}

Name of the automatically scanned Bean:

After automatic scanning is used, the bean ID is the Bean class name by default, but the first letter is lowercase. For example, in the preceding userbean, the bean ID is userbean, of course, you can specify the custom ID:

@ Component ("user ")

Public ClassUserbean {

Public VoidAdd ()

{

System.Out. Println ("add method is runing ~~~~~ ");

}

}

In this way, the bean ID is changed to user.

Scope of automatically scanned beans:

Bean managed by spring has the default scope of singleton. Other scopes can be required during development. Spring provides @ scope annotation for Configuration:

@ Scope ("prototype ")

@ Component ("user ")

Public ClassUserbean {

Public VoidAdd ()

{

System.Out. Println ("add method is runing ~~~~~ ");

}

}

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.