spring IOC 使用list数组注入

来源:互联网 发布:大溪淘宝拍照 编辑:程序博客网 时间:2024/05/20 15:11

一个接口和两个接口实现类

package com.imooc.beanannotation.multibean;public interface BeanInterface {}

实现类1

package com.imooc.beanannotation.multibean;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(2)@Componentpublic class BeanImplOne implements BeanInterface {}

实现类2

package com.imooc.beanannotation.multibean;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(1)@Componentpublic class BeanImplTwo implements BeanInterface {}

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"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd" >        <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan> </beans>
package com.imooc.beanannotation.multibean;import java.util.List;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;@Componentpublic class BeanInvoker {    //让bean的两个接口实现类注入到list和map中    @Autowired    private List<BeanInterface> list;    @Autowired    private Map<String, BeanInterface> map;    @Autowired    //因为有两个两个接口实现类,所以要指定一个,不然会报错     @Qualifier("beanImplTwo")    private BeanInterface beanInterface;    public void say() {        if (null != list && 0 != list.size()) {            System.out.println("list...");            for (BeanInterface bean : list) {                System.out.println(bean.getClass().getName());            }        } else {            System.out.println("List<BeanInterface> list is null !!!!!!!!!!");        }        System.out.println();        if (null != map && 0 != map.size()) {            System.out.println("map...");            for (Map.Entry<String, BeanInterface> entry : map.entrySet()) {                System.out.println(entry.getKey() + "      " + entry.getValue().getClass().getName());            }        } else {            System.out.println("Map<String, BeanInterface> map is null !!!!!!!!!!");        }        System.out.println();        if (null != beanInterface) {            System.out.println(beanInterface.getClass().getName());        } else {            System.out.println("beanInterface is null...");        }    }}
package com.imooc.test.beanannotation;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.BlockJUnit4ClassRunner;import com.imooc.beanannotation.injection.service.InjectionService;import com.imooc.beanannotation.multibean.BeanInvoker;import com.imooc.test.base.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)public class TestInjection extends UnitTestBase {    public TestInjection() {        super("classpath:spring-beanannotation.xml");    }    /*    @Test    public void testAutowired() {        InjectionService service = super.getBean("injectionServiceImpl");        service.save("This is autowired.");    }    */    @Test    public void testMultiBean() {        BeanInvoker invoker = super.getBean("beanInvoker");        invoker.say();    }}