Spring's IOC container study notes

(A) Spring's IOC learning

  In the bean applicationContext.xml configured, through the interface, in the main program, initialization of an object can be specified, no assignment is performed, configured directly in xml.

The next is to share a variety of methods bean configuration:

① get the bean class id

<bean id="user" class="com.author.bean.user">
        <property name="name" value="Spring"></property>
        <property name="age" value="18"></property>
        <property name="sex" value="男"></property>
</bean>

In the main function, to obtain a container by the bean object id

ApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
user beanUser=(user)aContext.getBean("user");
beanUser.print();

Output information of the object:

 

 

 ② acquired by the type of bean, the disadvantage is that only a type of bean object, if there are multiple error occurs, more of which may be obtained by way of id

User object such as the above, it can be acquired in the main function:

ApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
user beanUser=aContext.getBean(user.class);
beanUser.print();

The results obtained and the same as above

Second, the bean property assignment to various operations

① directly <property name = "name" value = "Spring"> </ property> using the name, value assignment manner

② assignments via the constructor, the premise in the bean class constructor

<bean id="user03" class="com.author.bean.user">
        <constructor-arg name="name" value="小明"></constructor-arg>
       <constructor-arg name="age" value="18"></constructor-arg>
       <constructor-arg name="sex" value="男"></constructor-arg>
</bean>

Name attribute can also directly be omitted:

<!-- 省略name属性 -->
    <bean id="user04" class="com.author.bean.user">
        <constructor-arg value="校花"></constructor-arg>
        <constructor-arg value="10"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
    </bean>

Position by the index value specified parameter

<bean id="book" class="com.atguigu.spring.bean.Book" >
           <constructor-arg value= "10010" index ="0"/>
           <constructor-arg value= "Book01" index ="1"/>
           <constructor-arg value= "Author01" index ="2"/>
           <constructor-arg value= "20.2" index ="3"/>
     </bean >

To distinguish it by the type constructor overloads

<bean id="book" class="com.atguigu.spring.bean.Book" >
      <constructor-arg value= "10010" index ="0" type="java.lang.Integer" />
      <constructor-arg value= "Book01" index ="1" type="java.lang.String" />
      <constructor-arg value= "Author01" index ="2" type="java.lang.String" />
      <constructor-arg value= "20.2" index ="3" type="java.lang.Double" />
</bean >

p namespace: To simplify the configuration XML file, an increasing number of XML documents using attributes instead of child elements of configuration information.

Need to import: xmlns: p = "http://www.springframework.org/schema/p"

<bean 
    id="studentSuper" 
    class="com.atguigu.helloworld.bean.Student"
    p:studentId="2002" p:stuName="Jerry2016" p:age="18" />

Third, the internal bean object and reference object

By <null /> element specifies empty

Reference to the object using: ref = "reference to an external object id"

<bean id="car01" class="com.author.bean.car">
        <constructor-arg value="宝马" ></constructor-arg>
         <constructor-arg value="蓝白色"></constructor-arg>
          <constructor-arg value="30000"></constructor-arg>
    </bean>
    
    <bean id="user05" class="com.author.bean.user">
        <property name="name">
            <null></null>
        </property>
        <!-- ref引用的是外部的bean -->
        <property name="car" ref="car01"></property> 
    </bean>

Internal bean objects: equivalent car = new car (), car object in the user object

<bean id="car01" class="com.author.bean.car">
        <constructor-arg value="宝马" ></constructor-arg>
         <constructor-arg value="蓝白色"></constructor-arg>
          <constructor-arg value="30000"></constructor-arg>
    </bean>
    
    <bean id="user05" class="com.author.bean.user">
        <property name="name">
            <null></null>
        </property>
        <!-- 相当于car=new car() 引用内部bean -->
        <property name="car">
            <bean class="com.author.bean.car">
                <property name="name" value="自行车"></property>
            </bean>
        </property>
    </bean>

Fourth, the collection property

①List and map

List the collection include: a custom objects, a reference to the object.

  Java.util.List configuration attribute type, specify the <list> tag, comprising a number of elements in the tag.

  These tags can be specified by a simple constant value <value>, designated by the references to other Bean <ref>.

  By <bean> Specifies built bean definition. Empty elements specified by <null />.

Map the collection comprising: two value, a reference, a custom object, corresponds to a different key value

  <Map> tag may be used a plurality of <entry> tag as a child. Each entry contains a key and a value.

 

  Must be defined in key <key> tag.

 

  Since the type and the key value is not limited, and can be freely specified <value>, <ref>, <bean> or <null /> elements thereof.

 

  Map key and may be a value defined as <entry>: Simple constant value using the key and defined; defined by reference to the bean key-ref and value-ref property.

 

<bean id="refbook" class="com.author.bean.Book">
        <property name="name" value="西厢记"></property>
    </bean>
    
    
    <bean id="user06" class="com.author.bean.user">
        <property name="books">
            <list>
                <!-- list标签体中添加每一个元素 -->
                <bean  class="com.author.bean.Book">
                    <property name="name" value="西游记"></property>
                    <property name="author" value="吴承恩"></property>
                    <property name="price" value="80"></property>
                </bean>
                <ref bean="refbook"></ref>
            </list>
        </property>
        <property name="maps">
            <map>
                <entry key="key01" value="张三"></entry>
                <entry key="key02" value="18"></entry>
                <entry key="key03" value-ref="refbook"></entry>
                <entry key="key04">
                    <bean class="com.author.bean.car">
                        <property name="name" value="宝马">
                        </property>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>

In the main function, the output:

user beanUser2=(user)aContext.getBean("user06");
        System.out.println(beanUser2.books);
        System.out.println(beanUser2.maps);

The results are:

 

 

 ②Properties

Use <props> defined java.util.Properties, a plurality of the tag <prop> tag as a child. Each <prop> tag must define key attributes

 

<bean class="com.atguigu.spring.bean.DataSource" id="dataSource">
    <property name="properties">
        <props>
            <prop key="userName">root</prop>
            <prop key="password">root</prop>
            <prop key="url">jdbc:mysql:///test</prop>
            <prop key="driverClass">com.mysql.jdbc.Driver</prop>
        </props>
    </property>
</bean>

 

③集合类型的bean

如果只能将集合对象配置在某个bean内部,则这个集合的配置将不能重用。我们需要将集合bean的配置拿到外面,供其他bean引用。

配置集合类型的bean需要引入util名称空间:xmlns:util="http://www.springframework.org/schema/util"

 

<util:map id="myMap">
        <entry key="key01" value="张三"></entry>
                <entry key="key02" value="18"></entry>
                <entry key="key03" value-ref="refbook"></entry>
                <entry key="key04">
                    <bean class="com.author.bean.car">
                        <property name="name" value="宝马">
                        </property>
                    </bean>
                </entry>
    </util:map>
    
    <util:list id="myList">
        <bean class="com.author.bean.Book"></bean>
        <value>12</value>
        <ref bean="myMap"></ref>
    </util:list>

 

五、级联属性

含义:可以控制属性的属性

可以改变user07下的car的price属性,如果car为引用对象,那么引用对象也会被修改

<!-- 级联属性可以控制属性的属性 -->
    <bean id="user07" class="com.author.bean.user">
        <property name="car" ref="car01"></property>
        <property name="car.price" value="900000"></property>
    </bean>

 

--------------这就是今天分享的bean的配置操作。

明天学习:利用工厂来创建bean

 

Guess you like

Origin www.cnblogs.com/xiaofengzai/p/12640003.html