课程链接:

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

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配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <context:component-scan base-package="com.imooc.beanannotation"/>
  11.  
  12. </beans>

测试类:

  1. package com.imooc.beaninvoker;
  2.  
  3. import org.junit.Test;
  4.  
  5. import com.imooc.beanannotation.injection.service.InjectionService;
  6. import com.imooc.beanannotation.multibean.BeanInvoker;
  7. import com.imooc.test.base.UnitTestBase;
  8.  
  9. public class TestBeanInvoker extends UnitTestBase{
  10.  
  11. public TestBeanInvoker() {
  12. super("classpath*:spring-beaninvoker.xml");
  13. }
  14.  
  15. @Test
  16. public void testBeanInvoker(){
  17. try {
  18. BeanInvoker bInvoker = super.getbean("beanInvoker");
  19. bInvoker.say();
  20. } catch (Exception e) {
  21. // TODO: handle exception
  22. e.printStackTrace();
  23. }
  24. }
  25. }

实体类:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;

  7. @Component
  8. public class BeanInvoker {
  9. @Autowired
  10. private List<BeanInterface> list;
  11. //
  12. public void say(){
  13. if(null!=list){
  14. for(BeanInterface bean :list){
  15. System.out.println(bean.getClass().getName());
  16. }
  17. }else{
  18. System.out.println("list is not null");
  19. }
  20.  
  21. }
  22.  
  23. }

dao类:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. public interface BeanInterface {
  4.  
  5. }

dao子类1:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component
  6. public class BeanImplOne implements BeanInterface{
  7.  
  8. }

dao子类2:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component
  6. public class BeanImplTwo implements BeanInterface{
  7.  
  8. }

打印结果:

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

2.2  map集合应用

实体类:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8.  
  9. @Component
  10. public class BeanInvoker {
  11. @Autowired
  12. private List<BeanInterface> list;
  13.  
  14. @Autowired
  15. private Map<String, BeanInterface> map;
  16. //
  17. public void say(){
  18. if(null!=list&&0!=list.size()){
  19. System.out.println("list...");
  20. for(BeanInterface bean :list){
  21. System.out.println(bean.getClass().getName());
  22. }
  23. }else{
  24. System.out.println("list is not null");
  25. }
  26.  
  27. if(null!=map&&0!=map.size()){
  28. System.out.println("map...");
  29. for(Map.Entry<String,BeanInterface> entry :map.entrySet()){
  30. System.out.println(entry.getKey()+" "+entry.getClass().getName());
  31. }
  32. }else{
  33. System.out.println("Map<String,BeanInterface> map is null");
  34. }
  35.  
  36. }
  37.  
  38. }

其他类完全相同:

打印日志:

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

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import org.springframework.core.annotation.Order;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Order()
  7. @Component
  8. public class BeanImplOne implements BeanInterface{
  9.  
  10. }

dao子类2:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import org.springframework.core.annotation.Order;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Order()
  7. @Component
  8. public class BeanImplTwo implements BeanInterface{
  9.  
  10. }

其他完全一致:

打印日志:

  1. 三月 18, 2019 7:00:13 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e41a882: startup date [Mon Mar 18 07:00:13 CST 2019]; root of context hierarchy
  3. 三月 18, 2019 7:00:13 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beaninvoker.xml]
  5. list...
  6. 三月 18, 2019 7:00:14 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
  7. 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e41a882: startup date [Mon Mar 18 07:00:13 CST 2019]; root of context hierarchy
  8. com.imooc.beanannotation.multibean.BeanImplTwo
  9. com.imooc.beanannotation.multibean.BeanImplOne
  10. map...
  11. beanImplOne java.util.LinkedHashMap$Entry
  12. beanImplTwo java.util.LinkedHashMap$Entry

Spring课程 Spring入门篇 4-3 Spring bean装配(下)之Autowired注解说明2 集合运用的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring @Autowired注解用在集合上面,可以保持接口的所有实现类

    CourseService课程接口有2个子类,HistroyCourseServiceImpl和MathsCourseServiceImpl public interface CourseServic ...

  4. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  5. Spring Data JPA 入门篇

    Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...

  6. Spring Boot源码(四):Bean装配

    为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...

  7. Spring学习十----------Bean的配置之Autowired注解实现

    © 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ...

  8. Spring课程 Spring入门篇 2-1 IOC和bean容器

    课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...

  9. spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

    [ 前言]  Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...

随机推荐

  1. kuangbin专题16H(next数组)

    题目链接: https://vjudge.net/contest/70325#problem/H 题意: 输入字符串 str, 求 str 子串中既是 str 前缀又是 str 后缀的的字符串长度, ...

  2. dmp文件恢复oracle数据库

    –创建用户 create user anhui identified by anhui -给予用户权限 grant create session to anhuigrant connect,resou ...

  3. 咕咕(数位dp+AC自动机)

    咕咕(数位dp+AC自动机) 若一个字符串的字符集合是0~m-1,那么称它为m进制字符串.给出n个m进制字符串\(s_i\),每个字符串的权值为\(v_i\).对于另一个m进制字符串\(S\),设\( ...

  4. [转]Log4j配置详解

    来自: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来设置记 ...

  5. 【NOIP 2015】斗地主

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的 A 到 K 加上大小王的共 54 张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: 3 ...

  6. 谁能赢呢? BZOJ 2463

    题目描述 小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的格子之前不能被访问 ...

  7. Django forum

    Django是比较有名的Python Web框架,很多著名的网站如Instagram就是用的Django.V2EX是一个界面简洁,功能丰富的论坛,最新源码尚未开源.网络上有很多模仿V2EX外观使用其它 ...

  8. linux上传与下载

    首先必须安装xshell这个工具 使用xshell来操作服务非常方便,传文件也比较方便.就是使用rz,sz首先,服务器要安装了rz,szyum install lrzsz当然你的本地windows主机 ...

  9. Qt 学习之路 2(4):信号槽

    Home / Qt 学习之路 2 / Qt 学习之路 2(4):信号槽 Qt 学习之路 2(4):信号槽  豆子  2012年8月23日  Qt 学习之路 2  110条评论 信号槽是 Qt 框架引以 ...

  10. datatables通过ajax调用渲染数据,怎么根据数据给td添加class

    html: <table id="table8" cellpadding="0" cellspacing="0" border=&qu ...