Spring4 Learning Note II: Bean related

Source: Internet
Author: User

One: The Bean configuration form

Based on XML configuration: Create a applicationcontext.xml file in the SRC directory, where it is configured.

Annotation-based configuration: When you create a bean class, the content is injected with annotations. (This is not good because the annotations are also in the code and are too fragmented)

Two: The way to find beans

Create a bean by reflection: Using the full class name when the bean is defined, the reflection mechanism is used to find the bean metadata and create the object. " therefore: The Bean class must have at least one parameterless constructor "

ID: The unique identity of the Bean object in the container, which can be called by ID in other objects in the container (for example, when the factory object is created, invoking a database connection pool object, and so on), or in code with a ApplicationContext object. Getbean (ID) To get a Bean object. If you do not specify an object, the class masterpiece is used by default as the ID.

Three: The assignment of the Bean object--the way of dependency injection

Attribute injection: When the bean is configured, through the <property> tag configuration, the name in the label specifies the property name, value specifies the property value, and the principle is: call the Setter method in the Bean class to assign the property value . A label assigns a property.

<bean id= "bean_id" class= "Www.ygj0930.bean.Bean" >        <property name= "attr1" value= "value1" ></ property>        <property name= "attr2" value= "value2" ></property>        <property name= "ATTR3" value = "Value3" ></property>        ......</bean>

Construct injection: When the bean is configured, the parameter value is configured for the constructor through the <constructor-arg> tag, value in the label specifies the parameter value, index specifies which parameter is the corresponding argument, and type describes the parameter type. A label assigns a parameter.

<bean id= "bean_id" class= "Www.ygj0930.bean.Bean" >        <constructor-arg value= "val0" index= "0" type= " Java.lang.XX "></constructor-arg>        <constructor-arg value=" Val1 "index=" 1 "type=" Java.lang.XX "> </constructor-arg>        <constructor-arg value= "Val2" index= "2" type= "Java.lang.XX" ></ Constructor-arg> .....    </bean>

property value, parameter value details:
1) If the attribute value, the parameter value contains the special symbol, you need to enclose the attribute value, the parameter value in C-data, namely <![ The cdata[value is configured in the form of]]>, which is interpreted in the XML by the special symbol, thus giving an error.

2) attributes, parameters are basic data types, encapsulated data types: You can use value= "literal" to inject.

3) attributes, parameters are other bean types:

3.1) referencing an external bean: If the attribute, the parameter variable value is an existing bean in the container, is injected using ref= "referenced bean_id".

3.2) Internal Bean: If the attribute, the parameter variable is a new bean created inside the current bean, is configured with the <property> tag, and a <bean> tag is embedded between the tags, and create an internal bean in a construction-injected manner.

<bean id= "External bean" >        <property name= "property name of internal Bean" >            <bean class= "full class name of internal Bean" >// internal Bean, No ID, cannot be referenced by external other bean               <constructor-arg value= "parameter value" index= "subscript" type= "parameter type" ></constructor-arg>                ......             </bean>        </property>
<property name= "Referencing the external Bean's property name" ref= "Other Bean's id" ></property>
...... </bean>

Constructs the internal bean parameter value creation method as above, the label is replaced by Constructor-arg.

IV: Cascading property configuration

If another bean1 is referenced as a member variable in a bean2, then we can assign a value to the referenced bean1 or modify the BEAN1 variable value when configuring BEAN2 through the member variable of the bean2.

Spring4 Learning Note II: Bean-related

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.