spring学习(14):Autowired的使用场景
CompactDisc类
package soundSystem;import org.springframework.stereotype.Component;@Component
public class CompactDisc {public CompactDisc() {super();System.out.println("compactdisc无参构造方法");}public void play(){System.out.println("正在播放音乐....");}
}
CDplay类
package soundSystem;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class CDPlayer {private CompactDisc cd;private Power power;public CDPlayer() {super();System.out.println("CDPlayer无参构造方法");}
/*@Autowiredpublic CDPlayer(CompactDisc cd) {this.cd = cd;System.out.println("CDPlayer有参构造方法");}*/
@Autowiredpublic CDPlayer(CompactDisc cd, Power power) {this.cd = cd;this.power = power;System.out.println("CDplayer的参数配置");}public void play(){power.supply();cd.play();}
}
appconfig类
package soundSystem;import org.springframework.context.annotation.ComponentScan;
//spring注解类
@ComponentScan
public class Appconfig {
}
power类
package soundSystem;import org.springframework.stereotype.Component;@Component
public class Power {public Power() {super();}public void supply(){System.out.println("电源宫殿中..");}
}
apptest类
package soundSystem;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;//@RunWith(SpringJunit4ClassRunner.class)
//@ContextConfiguration(class=)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Appconfig.class)
public class AppTest {@Autowiredprivate CDPlayer player;@Testpublic void testPlay(){player.play();}
}
运行结果
[INFO ] 2019-10-29 23:36:32,969 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.getDefaultTestExecutionListenerClassNames(AbstractTestContextBootstrapper.java:260)
Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
[INFO ] 2019-10-29 23:36:32,976 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209)
Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
[INFO ] 2019-10-29 23:36:32,977 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209)
Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
[INFO ] 2019-10-29 23:36:32,978 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209)
Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
[INFO ] 2019-10-29 23:36:32,979 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.getTestExecutionListeners(AbstractTestContextBootstrapper.java:187)
Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1786f9d5, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@704d6e83, org.springframework.test.context.support.DirtiesContextTestExecutionListener@43a0cee9][INFO ] 2019-10-29 23:36:33,076 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583)
Refreshing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Tue Oct 29 23:36:33 CST 2019]; root of context hierarchy
[DEBUG] 2019-10-29 23:36:33,086 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,086 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,105 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,108 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,173 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,173 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,175 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,195 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,195 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,195 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,197 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,202 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,210 method:org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:730)
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@158da8e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appconfig,CDPlayer,compactDisc,power]; root of factory hierarchy
[DEBUG] 2019-10-29 23:36:33,210 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,210 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,210 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
[DEBUG] 2019-10-29 23:36:33,211 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
[DEBUG] 2019-10-29 23:36:33,211 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
[DEBUG] 2019-10-29 23:36:33,214 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,217 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
[DEBUG] 2019-10-29 23:36:33,217 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
[DEBUG] 2019-10-29 23:36:33,218 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
[DEBUG] 2019-10-29 23:36:33,218 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,225 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
[DEBUG] 2019-10-29 23:36:33,226 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'appconfig'
[DEBUG] 2019-10-29 23:36:33,226 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'appconfig'
[DEBUG] 2019-10-29 23:36:33,227 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'appconfig' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,229 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'appconfig'
[DEBUG] 2019-10-29 23:36:33,229 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'CDPlayer'
[DEBUG] 2019-10-29 23:36:33,230 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'CDPlayer'
[DEBUG] 2019-10-29 23:36:33,243 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'compactDisc'
[DEBUG] 2019-10-29 23:36:33,244 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'compactDisc'
compactdisc无参构造方法
[DEBUG] 2019-10-29 23:36:33,245 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'compactDisc' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,248 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'compactDisc'
[DEBUG] 2019-10-29 23:36:33,251 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'power'
[DEBUG] 2019-10-29 23:36:33,252 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'power'
[DEBUG] 2019-10-29 23:36:33,255 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'power' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,258 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'power'
[DEBUG] 2019-10-29 23:36:33,259 method:org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:757)
Autowiring by type from bean name 'CDPlayer' via constructor to bean named 'compactDisc'
[DEBUG] 2019-10-29 23:36:33,260 method:org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:757)
Autowiring by type from bean name 'CDPlayer' via constructor to bean named 'power'
CDplayer的参数配置
[DEBUG] 2019-10-29 23:36:33,262 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'CDPlayer' to allow for resolving potential circular references
[DEBUG] 2019-10-29 23:36:33,264 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'CDPlayer'
[DEBUG] 2019-10-29 23:36:33,264 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'compactDisc'
[DEBUG] 2019-10-29 23:36:33,264 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'power'
[DEBUG] 2019-10-29 23:36:33,265 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
[DEBUG] 2019-10-29 23:36:33,329 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'lifecycleProcessor'
[DEBUG] 2019-10-29 23:36:33,338 method:org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:86)
Processing injected element of bean 'soundSystem.AppTest': AutowiredFieldElement for private soundSystem.CDPlayer soundSystem.AppTest.player
[DEBUG] 2019-10-29 23:36:33,339 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'CDPlayer'
[DEBUG] 2019-10-29 23:36:33,339 method:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.registerDependentBeans(AutowiredAnnotationBeanPostProcessor.java:535)
Autowiring by type from bean name 'soundSystem.AppTest' to bean named 'CDPlayer'
电源宫殿中..
正在播放音乐....
[INFO ] 2019-10-29 23:36:33,346 method:org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:984)
Closing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Tue Oct 29 23:36:33 CST 2019]; root of context hierarchy
[DEBUG] 2019-10-29 23:36:33,347 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'lifecycleProcessor'
[DEBUG] 2019-10-29 23:36:33,347 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:512)
Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@158da8e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appconfig,CDPlayer,compactDisc,power]; root of factory hierarchy
spring学习(14):Autowired的使用场景相关推荐
- Spring学习记录
Spring相关 目录 文章目录 Spring相关 目录 前言 工厂设计模式 静态工厂模式 通用工厂模式 ApplicationContext ClassPathXmlApplicationConte ...
- spring学习12 -Spring 框架模块以及面试常见问题注解等
以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式-在AOP和remoting中被用的比较 ...
- Spring学习(五)——Spring注解(一)
---恢复内容开始--- 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射 ...
- Spring学习总结一
Spring框架IoC与DI思想及应用 Spring学习总结一 1.Spring是什么 2.Spring的优点 2.1.关键概念 2.2.Spring的优点 3.Spring的架构图 3.1.核心容器 ...
- Spring学习笔记 之 Spring<全>
开始学习Spring全家桶 文章目录 1. IoC 定义 为什么叫控制反转? 实现 IoC 容器创建 bean 的两种⽅式 IoC DI 特殊字符的处理 Spring 中的bean创建类型 -- sc ...
- Spring——Spring学习教程(详细)(上篇)——IOC、AOP
本文是Spring的学习上篇,主要讲IOC和AOP. Spring的JDBCTemplete以及事务的知识,请见下篇. Spring--Spring学习教程(详细)(下篇)--JDBCTemplete ...
- Spring学习总结三
Spring框架JdbcTemplate与事务 Spring学习总结三 0.需要的jar包 1.JdbcTemplate模板 1.1.JdbcTemplateAPI的操作 1.1.1.User类 1. ...
- Spring学习(九)Spring 和数据库编程【了解】
本文借鉴:Spring学习,Spring框架总结 一.传统 JDBC 回顾 用一个大佬的demo来简单看一下 /*** 使用jdbc,根据id查询单个Student的信息*/ public class ...
- Spring学习(七)bean装配详解之 【通过注解装配 Bean】【自动装配的歧义解决】...
本文借鉴:Spring学习,@Bean 的用法(特此感谢!) 自动装配 1.歧义性 我们知道用@Autowired可以对bean进行注入(按照type注入),但如果有两个相同类型的bean在IOC容器 ...
- Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】
本文借鉴:Spring学习(特此感谢!) 通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XM ...
最新文章
- 【转】ubuntu下修改文件夹权限
- 浅谈机器人产品落地全过程
- 【学习笔记】redis一些配置文件参数详解
- android开机优化服务,Android开机速度优化简单回顾
- python逐行读取字符串_python3.4.3下逐行读入txt文本并去重的方法
- python3 爬虫例子_如何让你写的爬虫速度像坐火箭一样快【并发请求】
- 67、ulimit的使用
- flex item的width VS flex-basis
- 自动化集成:Jenkins管理工具详解
- Java自动计算迷宫正确路线算法源码
- eggjs增删改查MySQL,nodejs操作mysql实现增删改查
- 汇编语言——寻址方式的综合应用及转移指令的原理
- bwlabel和bwconncomp区别
- asp.net控件开发基础二
- docker(podman)命令参考
- 以$开头的shell脚本的变量
- android 按键上报流程,input子系统详解,按键如何上报
- Atitit 机器学习算法分类 目录 1. 传统的机器学习算法 vs 深度学习	1 1.1. 传统的机器学习算法包括决策树、聚类、贝叶斯分类、支持向量机、EM、Adaboost等等。	2 2. 监
- java实现文件夹的复制(包括子文件夹以及子文件)
- 带通滤波器的设计概述
热门文章
- 玩转12306之系统登录
- if ( document.all ) 可以简单的判断浏览器是否IE浏览器?
- MM看过来!教你如何打扮变成时尚达人 - 生活至上,美容至尚!
- outlook中的邮件归档
- hide show vue 动画_(Vue动效)7.Vue中动画封装
- 单因素方差分析_基于R语言开展方差分析(一)——单因素方差分析
- mysql 表ful,你所不知的table is full那些事
- 嵌入式linux系统移植的四大步骤_嵌入式系统移植步骤
- php起始符大全,PHP 符号大全
- 数据库杂谈(五)——关系数据库语言