Spring Introduction Study Notes (1.02) -- configure bean in Spring IoC container

Source: Internet
Author: User
Tags array definition

(1) Each bean should provide a unique name or ID and a fully qualified class name for the IOC container to instantiate it. You can specify a <value> element for each bean attribute of a simple type (such as string and other simple types. Spring will try to convert the value you specified to the declared type of this attribute. To configure an attribute through setter injection, you can use the <property> element and specify the attribute name in its name attribute. Note: Each <property> requires that the bean contain a corresponding setter method. As follows:

<bean name="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">   <property name="prefix">      <value>30</value>   </property>   <property name="suffix">      <value>A</value>   </property>   <property name="initial">      <value>100000</value>   </property></bean>
You can also declare it in the <constructor-Arg> element and configure bean attributes through constructor injection. The book says that <constructor-Arg> does not have the name attribute because the constructor parameters are location-based, but the latest version (I don't know from which version) in spring, the <constructor-Arg> element has the name attribute. As follows:
 <bean name="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">    <constructor-arg name="prefix">       <value>30</value>    </constructor-arg>    <constructor-arg name="suffix">       <value>A</value>    </constructor-arg>    <constructor-arg name="initial">       <value>100000</value>    </constructor-arg> </bean>
In fact, the preferred method for marking beans is: through the standard xml id attribute, if your text editor is XML aware, you can verify the uniqueness of each bean during design. <Bean id = "sequencegenerator" class = "com. Jackie. codeproject. springrecipesnote. springioc. sequencegenerator" & gt ;... </Bean> in the book, spring allows specifying multiple beans separated by commas in the name attribute, but this cannot be done in the ID attribute, because Commas are not allowed here. However, in the latest spring version, the ID attribute can contain special characters (including commas), but the name attribute cannot contain commas. In fact, both the bean name and bean ID are required. A bean with no name defined is called an anonymous bean. This bean is usually used only for interaction with spring containers. In this case, it will only be injected by type, or it will be embedded in an external bean declaration. (2) You can replace the attribute enclosed in the <value> element with the value attribute in the <value> element in the <constructor-Arg> element.
<Bean id = "sequencegenerator" class = "com. jackie. codeproject. springrecipesnote. springioc. sequencegenerator "> <property name =" prefix "value =" 30 "/> <property name =" suffix "value =" A "/> <property name =" initial "value = "100000"/>
 </bean>

<Bean name = "sequencegenerator" class = "com. jackie. codeproject. springrecipesnote. springioc. sequencegenerator "> <constructor-Arg name =" prefix "value =" 30 "/> <constructor-Arg name =" suffix "value =" A "/> <constructor-Arg name = "initial" value = "100000"/> </bean>

This is concise. Starting from spring 2.0, you can use P schema to define bean attributes as in the <bean> element. 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:p="http://www.springframework.org/schema/p"   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"><bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" p:prefix="30" p:suffix="A" p:initial="100000" /></beans>
(3) configure the collection of beans. To define the attributes of the Java. util. List interface in bean configuration, you must specify a <list> flag containing elements. The elements allowed in the <list> tag can be a constant value specified by <value>, <ref> specified bean reference, <bean> specified internal bean definition, and <idref> specified id reference definition, or <null> the specified null element. You can even embed another set in one set.
  <bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">     <property name="suffixes">        <list>           <value>A</value>           <bean class="java.net.URL">              <constructor-arg value="http" />              <constructor-arg value="www.apress.com" />              <constructor-arg value="/" />           </bean>           <null />        </list>     </property>     <property name="initial" value="100000" /></bean>
The array definition in the bean configuration file is the same as the list indicated by the <list> tag. To define the attributes of the Java. util. Set interface, use the <set> flag to define elements. The method is the same as that of list.
 <bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">    <property name="suffixes">       <set>          <value>A</value>          <bean class="java.net.URL">             <constructor-arg value="http" />             <constructor-arg value="www.apress.com" />             <constructor-arg value="/" />          </bean>          <null />       </set>    </property>    <property name="initial" value="100000" /> </bean>
Spring uses Java. util. javashashset to retain the order of elements. In spring, map is defined by the <map> tag with multiple <entry> tags as sub-projects. Each project contains a keyword and a value. Keywords must be defined in the <key> tag. There is no limit on the type of keywords and values, you can specify a <value>, <ref>, <bean>, <idref>, or <null> value for them. Spring will also use Java. util. linkedhashmap to retain the order of MAP projects.
 <bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">    <property name="suffixes">       <map>          <entry>             <key>                <value>type</value>             </key>             <value>A</value>          </entry>          <entry>             <key>                <value>url</value>             </key>             <bean class="java.net.URL">                <constructor-arg value="http" />                <constructor-arg value="www.apress.com" />                <constructor-arg value="/" />             </bean>          </entry>       </map>    </property>    <property name="initial" value="100000" /> </bean>
Keyword and value pairs can be defined using the <entry> tag attributes. For simple constants, the key and value attributes can be used. For references, the key-ref and value-ref attributes can be used for definition.
 <bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">    <property name="suffixes">       <map>          <entry key="type" value="A" />          <entry key="url" >             <bean class="java.net.URL">                <constructor-arg value="http" />                <constructor-arg value="www.apress.com" />                <constructor-arg value="/" />             </bean>          </entry>        </map>     </property>     <property name="initial" value="100000" /></bean>
The Java. util. properties set is very similar to map. It also implements the java. util. map interface and stores the project as a key-value pair. The only difference is that the keywords and values of the properties set are always strings. To define the java. util. properties set in spring, use the <props> tag and use multiple <prop> tags as sub-projects. Each <prop> tag must define a key attribute and contain the corresponding value.
 <bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">    <property name="suffixes">       <props>          <prop key="type">A</prop>          <prop key="url">http://www.apress.com/</prop>          <prop key="null">null</prop>       </props>    </property>    <property name="initial" value="100000" /> </bean>
If the inherited bean is used, the Child bean set can be merged with the parent bean by setting the merge attribute to true. For a list set, child elements are appended to the parent element in order. If the set or map set has the same value, the child element overwrites the parent element.

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.