Bean configuration in Spring and Spring Bean Configuration

Source: Internet
Author: User

Bean configuration in Spring and Spring Bean Configuration

Original works, can be reproduced, but please mark the source address http://www.cnblogs.com/V1haoge/p/5859556.html

 

1. Bean configurations are generally configured in XML files.

2. Bean packages: org. springframework. beans and org. springframework. context

3. spring Bean management relies on a large amount of reflection.

4. Bean definition Configuration

4.1Bean tag

  Id attribute: Used to specify the Bean name, used when the Bean is dependent, used when obtaining the Bean, etc.

  Name attribute: Used to specify the Bean alias

  Class attributes: Used to specify the Bean source, that is, to create the class of the Bean to be created (requires a fully qualified name)

  Singleton attributes: Used to specify the Creation Mode of the current Bean. If the value is true, it indicates the singleton mode, and false indicates the prototype)

  Depends-on Attributes: Used to specify the dependent Bean of the current Bean. The specified Bean is initialized before the initialization of the current Bean.

  Init-method attributes: Used to specify the initialization method of the current Bean. After the Bean instance is created, the method with the specified name is called first.

  Destory-method attributes: Used to specify the destroy method of the current Bean. The method specified by this attribute is automatically called before the Bean is destroyed.

  Lazy-init attributes: Specifies the initialization Time of the current Bean. If the value is true, the instance will be automatically created and initialized during the initial call. If the value is false, the creation and initialization will be completed when the IoC container is created.

  Autowire attributes: Specifies the automatic injection mode for the dependencies of the current Bean. It has five values:

BytesByNameValue: Indicates automatic match by id name;

BytesByTypeValue: Indicates Automatic Assembly Based on the type specified by the class;

BytesConstructorValue: Indicates automatic assembly using the parameters of the constructor (parameter type matching );

BytesAutodetectValue: Indicates that the matching method is automatically selected. constructor is automatically assembled first. If no constructor is automatically assembled in byType mode;

BytesNoValue: Indicates that automatic assembly is not applicable.

  Dependency-check attributes: It is used to specify the Bean dependency Check Mode and check whether the dependency is complete. It is used with automatic assembly and has four values:

BytesSimpleValue: Checks the dependencies of basic types, strings, and sets.

BytesObjectValue: Checks the dependencies of referenced objects.

BytesAllValue: Checks the dependencies of basic types, strings, sets, and referenced objects.

BytesNoneValue: Indicates that no dependency check is performed. The default value is true.

Bean example:

 

 1 <?xml version=”1.0”encoding=”utf-8”?>  2 <!Doctype beans PUBLIC “-//SPRING//DTD BEAN//EN” 3 “http://www.springframework.org/dtd/spring-beans.dtd”> 4 <beans> 5   <bean id=”helloworld” class=”com.dh.spring.HelloWorld” singleton=”true” depends-on=”date” lazy-init=”false” init-mathod=”init” destory-method=”destory”/> 6   <bean id=”date” class=”java.util.Date”/> 7 </beans>

4.2Property tag

  Name attribute: Used to specify the attribute name, which is the same as the name after the set method in the class.

  Value Attribute: Specifies the value of this attribute. It is used to specify the basic type and string type.

  Ref attribute: Used to specify the value of this attribute. The specified value is the reference object type (other Bean), and the value after ref is the id of another Bean.

  ValueTag: Used to specify the attribute value. The type is basic type and string type. The value is the text content in the tag. You can use null to set the attribute value to null.

  RefTag: Used to specify the value of an attribute. The type is the type of the referenced object and the value is the value of its attribute. There are three types of attributes:

BytesLocal Properties: Used to specify the dependency on the local Bean instance, that is, the Bean defined in the same XML file.

BytesBean attributes: Used to specify the dependent Bean instance. It can be a Bean in different XML files.

BytesParent attributes: Used to specify the dependent Bean instance. It can be the Bean in the current BeanFactory or ApplicationContext parent BeanFactory or ApplicationContext.

The following are tags for the set:

  ListTag: Declares that the dependent object is a list set. The value and ref labels are used to specify values (basic, String, object, etc.) in the list)

BytesValueTag: Used to specify the value in the list set. The specified value is of the basic type and string type, and the value is text content.

BytesRefTag: Used to specify the reference value in the list set. The specified value is another object Bean. Its usage is the same as that of the ref tag under the property tag.

  SetTag: Declares that the dependent object is a set. Its usage is the same as that of the list tag.

  MapTag: Declares that the dependency object is a map set and uses the entry Label to declare a key-value pair.

BytesEntryTag: Declares a key-value pair in the map set. The key attribute is used to specify the key, and the value/ref label specifies the value.

Key Attribute: Specifies the key in a key-value pair, which is generally a string

ValueTag: Specifies the value in the key-value pair. The type is basic type or string type.

RefTag: Specifies the value in the key-value pair. The type is the reference object type, that is, other beans. Its usage is the same as that of the previous ref tag.

Map instance 1:

 1 <bean id=”helloworld” class=”com.dh.spring.HelloWorld”> 2   <property name=”pname”> 3     <map> 4       <entry key=”mkey1”> 5         <value>mvalue1</value> 6       </entry> 7       <entry key=”mkey2”> 8         <value>mvalue2</value> 9       </entry>10     </map>11   </property>12 </bean>

Map instance 2:

1 <bean id=”helloworld2” class=”com.dh.spring.HelloWorld2”>2     <property name=”pname”>3       <map>4       <entry key=”mkey1”>5         <ref bean=”helloworld”/>6       </entry>7     </map>8   </property>9 </bean>    

  PropsTag: Declares that the dependency object is a properties set, and uses the prop label to specify the attribute name and value (key-Value Pair)

BytesPropTag: Used to set a key-value pair in the Set

Key Attribute: Specifies the key in the key-value pair, which is generally a string

Text Content: Specifies the value in a key-value pair, which is generally a string without quotation marks.

Props instance:

1 <bean id=”helloword” class=”com.dh.spring.HelloWorld”>2   <property name=”pname”>3     <props>4       <prop key=”pkey1”>pvalue1</prop>5       <prop key=”pkey2”>pvalue2</prop>6     </props>7   </property>8 </bean>

5. Bean Lifecycle

Bean lifecycle includes Bean definition, Bean initialization, Bean use, and Bean destruction.

Bean definition: Generally, beans are defined using XML files. dependencies between beans and attribute values are defined during definition.

Bean initialization: in fact, Bean initialization includes two methods: Bean creation and initialization. Bean creation and initialization are generally performed synchronously, and Bean initialization will be performed directly after creation, the Creation Time is related to the setting of the Bean's lazy-init attribute.

Bean usage: This Bean instance will be used when a Bean is called during the web program running. If you use the encoding method to obtain the Bean, it is also Bean usage, there are three Bean encoding methods: BeanWarpper, BeanFactory, and ApplicationContext.

Bean destruction: the Bean instance is destroyed when the program exits, and the method with the name specified by the destory-method attribute of the Bean is automatically called before destruction.

Related Article

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.