spring集合屬性的注入學習筆記

applicationContext.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">

    <!-- 配置Service -->
    <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
        <!-- 注入DAO的屬性 -->
        <property name="customerDao" ref="customerDao"/>
    </bean>

    <!-- 配置DAO -->
    <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">

    </bean>

</beans>

實體對象

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * @author lw
 * @createTime 2018/10/12 10:28
 * @description 集合屬性的注入:
 */
public class CollectionBean {

    private String[] arrs;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;

    public void setArrs(String[] arrs) {
        this.arrs = arrs;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map
                + "]";
    }

}

測試類

import com.pojo.CollectionBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author lw
 * @createTime 2018/10/12 10:20
 * @description  集合
 */
public class SprintDome {

    @Test
    public void demo1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/pojo/applicationContexolad.xml");
        CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
        System.out.println(collectionBean);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章