Inherit Bean configuration

Source: Internet
Author: User

Inherit Bean configuration

Spring allows the inheritance of the bean's configuration, and the inherited Bean is called the parent bean. The Bean that inherits this parent bean is called a child bean.
The child bean inherits the configuration from the parent bean, including the Bean's property configuration
The child bean can also overwrite the configuration inherited from the parent bean.
The parent bean can be either a configuration template or a bean instance. If you only want to use the parent bean as a template, you can set the abstract property of <bean> to True so that Spring will not instantiate the bean
Not all attributes in the <bean> element will be inherited. For example: Autowire, abstract and so on.
You can also ignore the class property of the parent bean, let the child bean specify its own class, and share the same property configuration. However, abstract must be set to true at this time

Dependent Bean Configuration

Spring allows the user to set the bean's predecessor-dependent bean through the Depends-on property, which is created before the bean is instantiated by the predecessor-dependent bean.
If the predecessor relies on more than one bean, you can configure the bean's name by commas, spaces, or by the way

Scope of the Bean

In Spring, you can set the scope of the bean in the scope property of the <bean> element.
By default, Spring creates only one instance for each Bean declared in the IOC container, which can be shared across the scope of the IOC container: all subsequent getbean () calls and bean references will return this unique instance of the bean. This scope is called sin Gleton, which is the default scope for all beans.

Using the external properties file

When configuring beans in a configuration file, it is sometimes necessary to mix the details of the system deployment in the bean's configuration (for example: file path, data source configuration information, etc.). And these deployment details actually need to be separated from the Bean configuration
Spring provides a propertyplaceholderconfigurer Beanfactory post processor that allows the user to move part of the Bean configuration out of the properties file. You can use variables of the form ${var} in the Bean configuration file, propertyplaceholderconfigurer the dependency file, and use these properties to replace the variables.
Spring also allows the use of ${propname} in property files to implement mutual references between attributes.

Register Propertyplaceholderconfigurer

Spring 2.0:

After Spring 2.5: Simplified by <context:property-placeholder> elements:
Add a context Schema definition in <beans>
Add the following configuration to the configuration file:

Spring Expression Language: Spel

Spring expression Language (Spel): is a powerful expression language that supports run-time querying and manipulating object graphs.
Syntax similar to El:spel using #{...} as delimiters, all characters in the large frame number will be considered spel
Spel provides the convenience for dynamic assignment of bean properties
The Spel can be implemented by:
To reference a bean by its ID
Calling methods and referencing properties in an object
Evaluate the value of an expression
Matching of regular expressions

Spel: Literal

The representation of a literal:
Integer: <property name= "Count" value= "#{5}"/>
Decimals: <property name= "Frequency" value= "#{89.7}"/>
Scientific notation: <property name= "Capacity" value= "#{1e4}"/>
String can use single or double quotation marks as the bounding symbol of a string: <property name= "name" value= "#{' Chuck '}"/> or <property name= ' name ' value= ' #{' Chuck "} '/>
Boolean:<property name= "Enabled" Value= "#{false}"/>

Spel: Referencing Beans, properties, and methods (1)

Referencing other objects:

Referencing properties of other objects

Call other methods, and you can also chain-operate

Spel supported operational symbols (1)

Arithmetic operators: +,-, *,/,%, ^:

The plus sign can also be used as a string connection:

Comparison operators: <,;, = =, <=, >=, lt, GT, eq, le, ge

Spel Supported operational symbols (2)

Logical operation symbols: and, or, not, |


If-else operator:?: (Ternary),?: (Elvis)


Variants of the If-else


Regular expression: Matches

Spel: Referencing Beans, properties, and methods (2)

Call a static method or static property: Calls a static method of a class through T (), it returns a class Object, and then calls the appropriate method or property:

The life cycle method of the bean in the IOC container spring The IOC container manages the bean's life cycle, and Spring allows custom tasks to be performed at a specific point in the Bean's lifecycle. Spring IOC Container management of the Bean's lifecycle: – By constructing Or factory method to create a bean instance – Set a value for the Bean's property and a reference to the other bean – the initialization method for invoking the bean –bean can be used – when the container is closed, call the Bean's destruction method • Set Init-method and de in the Bean's declaration The Stroy-method property that specifies the initialization and destruction methods for the Bean. Creating a bean Post processor Bean The Post processor allows additional processing of the bean before and after the initialization method is called. The Bean processor handles all bean instances in the IOC container one by one, rather than a single instance. The typical application is to check the correctness of bean properties or change the Bean's properties according to specific criteria.

• for the Bean post processor, the interface needs to be implemented . Before and after the initialization method is called, Spring will pass each bean instance to the following two methods of the above interface:

  Add bean after processor the lifetime of the Bean spring the IOC container to manage the life cycle of the bean: – Create a bean instance from a constructor or factory method – Set a value for the Bean's property and a reference to another bean – the bean is actually Example of the Postprocessbeforeinitialization method passed to the bean's post processor – invoking the Bean's initialization method – passing the bean instance to the postprocessafterinitializatio of the Bean's post processor The N method –bean can be used – when the container is closed, invoking the Bean's destruction method by calling the static factory method to create a bean call to the static factory method to create the bean is to encapsulate the object creation process into a static method. When the client needs an object, it simply calls the static method, and the details of creating the object are different. • To declare a bean created through a static method, you need to specify the class of the method that owns the factory in the Bean's class attribute, and specify the factory method in the Factory-method attribute. Name. Finally, using the <constrctor-arg> element to pass method parameters for this method .  creates the Bean instance factory method by invoking the instance factory method: Encapsulates the object's creation process into another object instance's method. When a client needs to request an object, it simply calls the instance method without caring about the object's creation details. • To declare a bean– created by an instance factory method specify the bean– in the Bean's Factory-bean attribute that owns the factory method in the Factory-method property specifies the name of the factory method – Use the Construtor-arg element to pass method parameters to the factory method    implement the Factorybean interface there are two types of beans in the Spring IOC container configuration Bean spring, one is a generic bean, and the other is a factory bean, the Factorybean. factory Bean is different from a normal bean, and its returned object is not an instance of the specified class, it returns the object returned by the factory Bean's GetObject method   & Nbsp;                  

Inherit Bean configuration

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.