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()
@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()
@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
Spring课程 Spring入门篇 4-3 Spring bean装配(下)之Autowired注解说明2 集合运用的更多相关文章
- Spring Boot -01- 快速入门篇(图文教程)
		Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ... 
- Spring实践系列-入门篇(一)
		本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ... 
- Spring @Autowired注解用在集合上面,可以保持接口的所有实现类
		CourseService课程接口有2个子类,HistroyCourseServiceImpl和MathsCourseServiceImpl public interface CourseServic ... 
- Spring Cloud Alibaba入门篇
		学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ... 
- Spring Data JPA 入门篇
		Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ... 
- Spring Boot源码(四):Bean装配
		为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ... 
- Spring学习十----------Bean的配置之Autowired注解实现
		© 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ... 
- Spring课程 Spring入门篇 2-1 IOC和bean容器
		课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ... 
- spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】
		[ 前言] Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ... 
随机推荐
- 【TED演讲】阿帕玛・饶:(幽默的高科技艺术)
			身为艺术家和TED Fellow的阿帕玛・饶对熟悉的事物以惊奇的幽默的方式进行再次想像.通过和索伦・普尔兹的合作,她创作出一系列高科技的艺术作品-一个会发邮件的打字机,一个让你在屏幕上消失而跟踪拍摄你 ... 
- 【图灵学院01】Java程序员开发效率工具IntelliJ IDEA使用
			1. 什么是IDEA? IDEA, Java智能IDE. 2. 为什么要使用? IDEA的优点: 1)智能选取 2)导航模式 3)历史记录 4)重构 5)编码辅助 6)智能排版,控制 7)智能代码,查 ... 
- Python项目完成
			今天,来对之前自学python做的一个小任务进行复习和总结,首先回顾之前任务的每一个步骤,按照自己个人的思路,一布一步往下做 需求:根据主网站,爬取出大连市各年地区生产总值,观察2010至2015年的 ... 
- linux curl命令:curl: (7) couldn't connect to host ?
			linux curl命令:curl: (7) couldn't connect to host ? 使用linux命令 curl http://www.test.com 出现如下错误:curl: (7 ... 
- Go语言基础环境配置(windows)
			一.基础软件包安装 需要安装go环境包.git.IDE(VScode),安装包见下图: 1.1 安装go windows环境直接双击安装包安装即可,在cmd窗口输入go,结果如下图所示即表示安装成功: ... 
- layui的表单功能
			作为一个phper还是非常喜欢这个插件的~虽然在vue的群里面说这个插件好被人怼过..废话不多说, 这次使用到的是layui的表单功能.上次的日历忘记做笔记了非常可惜,大部分其实跟着文档撸就可以,这次 ... 
- CENTOS 7 install mariadb 10.3
			CENTOS install mariadb 10.3 cat >/etc/yum.repos.d/MariaDB.repo << 'EOF' [mariadb] name = Ma ... 
- The 'gridview' module MUST be setup in your Yii configuration file.
			解决方法:common的config的main.php中添加 'gridview' => ['class' => 'kartik\grid\Module'], 在vender的compos ... 
- 29-----BBS论坛
			BBS论坛(二十九) 29.帖子详情页布局 (1)front/hooks.py @bp.errorhandler def page_not_found(): return render_templat ... 
- java——设计一个支持push,pop,top、在恒定时间内检索最小元素的栈。
			普通方法: 需要另外一个栈 用来存放每一时刻的min值 巧妙版: 只需要一个stack,stack中存的是与min的差值 但由于min是两个整数之间的差值,有可能会出现差值超过整数边界值的情况,因此要 ... 
