集合对象值的注入-IOC-Bean对象属性值注入(依赖注入)



    
        
            
                张三
                李四
                王五
            
        
        
            
                
                
                
            
        
        
            
                com.mysql.jdbc.Driver
                jdbc:mysql:///test
            
        
    



       
    

package utils;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Container {
    /**
     * 类里的属性还可看成容器:如屋子里的酒柜,屋子是容器,酒柜也是容器
     */
    private List list;
    private Map map;
    private Properties props;

    public List getList() {
        return list;
    }

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

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Properties getProps() {
        return props;
    }

    public void setProps(Properties props) {
        this.props = props;
    }

    @Override
    public String toString() {
        return "Container [list=" + list + ", map=" + map + ", props=" + props + "]";
    }

}

package test;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBase {
    protected ClassPathXmlApplicationContext ctx;
    @Before
    public void init(){
        ctx=new ClassPathXmlApplicationContext("spring-configs.xml");
    }
    @After
    public void close(){
        ctx.close();
    }
}
package test;
import org.junit.Assert;
import org.junit.Test;

import utils.Container;

public class TestContainer01 extends TestBase {
    @Test
    public void testContainer(){
        Container container = ctx.getBean(Container.class);
        Assert.assertNotEquals(null, container);
        System.out.println(container.getList());
        System.out.println(container.getMap());
        System.out.println(container.getProps());
    }
}

运行结果

[张三, 李四, 王五]
{chinese=90, english=85, java=100}
{url=jdbc:mysql:///test, driver=com.mysql.jdbc.Driver}

你可能感兴趣的:(集合对象值的注入-IOC-Bean对象属性值注入(依赖注入))