SpringDay01

 

Spring的控制反转

Spring的依赖注入

  多种注入方式

  多种属性的注入方式

<bean id="userDao" class="dao.UserDaoImpl"></bean>


<!-- 构造方法的方式注入属性 -->
<bean id="car" class="entity.Car">
    <constructor-arg name="name" value="保时捷"></constructor-arg>
    <constructor-arg name="price" value="1000000"></constructor-arg>
</bean>

<!-- set方法的方式注入属性 -->
<bean id="car2" class="entity.Car2">
    <property name="name" value="奇瑞QQ"></property>
    <property name="price" value="40000"></property>
</bean>


<!-- 注入对象类型的注入 -->
<bean id="person" class="entity.Person">
    <property name="name" value="张三"></property>
    <property name="car" ref="car"></property>
</bean>


<!-- Spring的复杂类型的注入 -->
<bean id="collectionBean" class="entity.CollectionBean">
    <!-- 数组类型的属性 -->
    <property name="array">
        <list>
            <value>张三</value>
            <value>李四</value>
            <value>王五</value>
        </list>
    </property>
    
    <!-- 注入List集合的数据 -->
    <property name="list">
        <list>
            <value>张三</value>
            <value>李四</value>
            <value>王五</value>
        </list>
    </property>
    
    <!-- 注入Map集合 -->
    <property name="map">
        <map>
            <entry key="aaa" value="111"></entry>
            <entry key="bbb" value="222"></entry>
            <entry key="ccc" value="333"></entry>
        </map>
    </property>
</bean>
package test;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.UserDao;
import entity.Car;
import entity.Car2;
import entity.CollectionBean;
import entity.Person;

public class Test1 {

    @Test
    public void demo(){
        //创建Spring的工厂类:
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过工厂解析XML获取Bean的实例.
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.sayHello();
        
        Car car = (Car) applicationContext.getBean("car");
        System.out.println(car.getName());
        System.out.println(car.getPrice());
        
        System.out.println("---------------------------");
        
        Car2 car2 = (Car2) applicationContext.getBean("car2");
        System.out.println(car2.getName());
        System.out.println(car2.getPrice());
        
        
        System.out.println("---------------------------");
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person.getName());
        System.out.println(person.getCar().getName());
        
        System.out.println("---------------------------");
        CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
        System.out.println(collectionBean.getArray().length);
        System.out.println(collectionBean.getList().toString());
        System.out.println(collectionBean.getMap().toString());
    }
}

 

posted @ 2019-02-26 17:17  程序员deepz  阅读(122)  评论(0编辑  收藏  举报