2019-11-07-IOC注入方式

IOC注入多种复杂类型

简单类型注入

Person.java

public class Person {
    private Integer id;
    private String name;
    private String gender;
    ...
}

ioc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "Person01" class="entity.Person">
        <property name="id" value="1151301118"></property>
        <property name="name" value="linjinbao"></property>
        <property name="gender" value="男"></property>
    </bean>
</beans>

注入另一个bean

Person.java

public class Person {
    private Integer id;
    private String name;
    private String gender;
    private Car car;    //另外一个bean
    ...
}

Car.java

public class Car {
    private String name;
    private double price;
    ...
}

ioc.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "Person01" class="entity.Person">
        <property name="id" value="1151301118"></property>
        <property name="name" value="linjinbao"></property>
        <property name="gender" value="男"></property>
        <property name="car">
            <bean class="entity.Car">
                <property name="name" value="宝马"></property>
                <property name="price" value="100000.00"></property>
            </bean>
        </property>
    </bean>
</beans>

注入另一个bean(方式二,引用ref)

...
<bean id = "Person02" class="entity.Person">
    <property name="id" value="1151301119"></property>
    <property name="name" value="linjinbao66"></property>
    <property name="gender" value="女"></property>
    <property name="car" ref="Car01"></property>
</bean>

注入复杂类型 包括List 和 Map

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "Person03" class="entity.Person">
        <property name="id" value="1151301120"></property>
        <property name="name" value="linjinbao666"></property>
        <property name="gender" value="男"></property>
        <property name="car" ref="Car01"></property>
        <property name="books">
            <list>
                <bean class="entity.Book">
                    <property name="name" value="西游记"></property>
                    <property name="price" value="100"></property>
                    <property name="author" value="吴承恩"></property>
                </bean>
                <bean class="entity.Book">
                    <property name="name" value="西游记"></property>
                    <property name="price" value="100"></property>
                    <property name="author" value="吴承恩"></property>
                </bean>
            </list>
        </property>
        <property name="maps">
            <map>
                <entry key="msg" value="hello"></entry>
                <entry key="car">
                    <bean class="entity.Car">
                        <property name="name" value="桑塔纳"></property>
                        <property name="price" value="100000.00"></property>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>


</beans>

总结

SpringIOC反向代理, 通过解析xml配置文件,来加载bean,实体类中的属性体现在bean中就是

重点: 复杂类型不能直接在value中体现

  1. 简单类型直接:<property name="name" value="西游记"></property>

  2. 自己定义的bean类型:

    <bean class="entity.Book">
     <property name="name" value="西游记"></property>
     <property name="price" value="100"></property>
     <property name="author" value="吴承恩"></property>
    </bean>
    
  3. map, list, set等复杂类型:

    <property name="maps">
     <map>
         <entry key="msg" value="hello"></entry>
         <entry key="car">
             <bean class="entity.Car">
                 <property name="name" value="桑塔纳"></property>
                 <property name="price" value="100000.00"></property>
             </bean>
         </entry>
     </map>
    </property>