• Spring课程 Spring入门篇 4-3 Spring bean装配(下)之Autowired注解说明2 集合运用


    课程链接:

    本节主要讲了以下几块内容

    1    注解相关解析

    2    代码演练

    集合for循环的使用

    2.1  list集合应用

    2.2  map集合应用

    2.3  集合排序(只对list有效,对map无效(list有序,map无序))

     

    1    注解相关解析

    1.1  autowired注解应用到集合属性可以提供所有特定类型的bean

    1.2  autowired可以提供集合中实体类的bean和子类的bean(继承,详情见下方代码)

    autowired注解不能应用在Spring BeanPostProcessor或者BeanFactoryPostProcessor中,必须用xml或@Bean方式进行注解加载(否则会造成循环依赖)

    2    代码演练

    2.1  list集合应用

    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"/>
    
    </beans>

    测试类:

    package com.imooc.beaninvoker;
    
    import org.junit.Test;
    
    import com.imooc.beanannotation.injection.service.InjectionService;
    import com.imooc.beanannotation.multibean.BeanInvoker;
    import com.imooc.test.base.UnitTestBase;
    
    public class TestBeanInvoker extends UnitTestBase{
    
    
        public TestBeanInvoker() {
            super("classpath*:spring-beaninvoker.xml");
        }
        
        @Test
        public void testBeanInvoker(){
            try {
                BeanInvoker bInvoker = super.getbean("beanInvoker");
                bInvoker.say();
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    实体类:

    package com.imooc.beanannotation.multibean;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanInvoker {
        @Autowired
        private List<BeanInterface> list;
        
        //
        public void say(){
            if(null!=list){
                for(BeanInterface bean :list){
                    System.out.println(bean.getClass().getName());
                }
            }else{
                System.out.println("list is not null");
            }
            
        }
    
    }

    dao类:

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

    dao子类1:

    package com.imooc.beanannotation.multibean;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanImplOne implements BeanInterface{
    
    }

    dao子类2:

    package com.imooc.beanannotation.multibean;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanImplTwo implements BeanInterface{
    
    }

    打印结果:

    三月 13, 2019 6:39:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4a8927c8: startup date [Wed Mar 13 06:39:28 CST 2019]; root of context hierarchy
    三月 13, 2019 6:39:29 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beaninvoker.xml]
    com.imooc.beanannotation.multibean.BeanImplOne
    com.imooc.beanannotation.multibean.BeanImplTwo
    三月 13, 2019 6:39:31 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4a8927c8: startup date [Wed Mar 13 06:39:28 CST 2019]; root of context hierarchy

    2.2  map集合应用

    实体类:

    package com.imooc.beanannotation.multibean;
    
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanInvoker {
        @Autowired
        private List<BeanInterface> list;
        
        @Autowired
        private Map<String, BeanInterface> map;
        
        //
        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 is not null");
            }
            
            if(null!=map&&0!=map.size()){
                System.out.println("map...");
                for(Map.Entry<String,BeanInterface> entry :map.entrySet()){
                    System.out.println(entry.getKey()+"  "+entry.getClass().getName());
                }
            }else{
                System.out.println("Map<String,BeanInterface> map is null");
            }
            
        }
    
    }

    其他类完全相同:

    打印日志:

    三月 18, 2019 6:52:15 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7292e1b2: startup date [Mon Mar 18 06:52:15 CST 2019]; root of context hierarchy
    三月 18, 2019 6:52:16 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beaninvoker.xml]
    list...
    com.imooc.beanannotation.multibean.BeanImplOne
    com.imooc.beanannotation.multibean.BeanImplTwo
    map...
    beanImplOne  java.util.LinkedHashMap$Entry
    beanImplTwo  java.util.LinkedHashMap$Entry
    三月 18, 2019 6:52:20 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7292e1b2: startup date [Mon Mar 18 06:52:15 CST 2019]; root of context hierarchy

    2.3  集合排序(只对list有效,对map无效(list有序,map无序))

    dao子类1:

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

    dao子类2:

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

    其他完全一致:

    打印日志:

    三月 18, 2019 7:00:13 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e41a882: startup date [Mon Mar 18 07:00:13 CST 2019]; root of context hierarchy
    三月 18, 2019 7:00:13 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beaninvoker.xml]
    list...
    三月 18, 2019 7:00:14 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e41a882: startup date [Mon Mar 18 07:00:13 CST 2019]; root of context hierarchy
    com.imooc.beanannotation.multibean.BeanImplTwo
    com.imooc.beanannotation.multibean.BeanImplOne
    map...
    beanImplOne  java.util.LinkedHashMap$Entry
    beanImplTwo  java.util.LinkedHashMap$Entry
  • 相关阅读:
    谈谈编译和运行
    全国车辆违章查询API文档及demo
    两款模拟键盘输入和鼠标点击的命令行工具
    利用AFNetworking框架去管理从聚合数据上面请求到的数据
    谈 API 的撰写
    谈 API 的撰写
    (四)Oracle条件查询,分页查询
    (三)Oracle字符串操作
    (二)Oracle数据库原理
    (一)Oracle安装详解
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/10520700.html
Copyright © 2020-2023  润新知