spring学习14-Autowired的使用场景

简介: 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无参构造方法");
         }
     /*@Autowired
         public CDPlayer(CompactDisc cd) {
             this.cd = cd;
             System.out.println("CDPlayer有参构造方法");
         }*/
     @Autowired
         public 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 {
         @Autowired
         private CDPlayer player;
         @Test
         public 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.DefaultListableB
相关文章
|
1月前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
8月前
|
监控 Java 应用服务中间件
微服务——SpringBoot使用归纳——为什么学习Spring Boot
本文主要探讨为什么学习Spring Boot。从Spring官方定位来看,Spring Boot旨在快速启动和运行项目,简化配置与编码。其优点包括:1) 良好的基因,继承了Spring框架的优点;2) 简化编码,通过starter依赖减少手动配置;3) 简化配置,采用Java Config方式替代繁琐的XML配置;4) 简化部署,内嵌Tomcat支持一键式启动;5) 简化监控,提供运行期性能参数获取功能。此外,从未来发展趋势看,微服务架构逐渐成为主流,而Spring Boot作为官方推荐技术,与Spring Cloud配合使用,将成为未来发展的重要方向。
323 0
微服务——SpringBoot使用归纳——为什么学习Spring Boot
|
5月前
|
安全 Java 数据库
Spring Boot 框架深入学习示例教程详解
本教程深入讲解Spring Boot框架,先介绍其基础概念与优势,如自动配置、独立运行等。通过搭建项目、配置数据库等步骤展示技术方案,并结合RESTful API开发实例帮助学习。内容涵盖环境搭建、核心组件应用(Spring MVC、Spring Data JPA、Spring Security)及示例项目——在线书店系统,助你掌握Spring Boot开发全流程。代码资源可从[链接](https://pan.quark.cn/s/14fcf913bae6)获取。
836 2
|
7月前
|
Java Spring
Spring框架的学习与应用
总的来说,Spring框架是Java开发中的一把强大的工具。通过理解其核心概念,通过实践来学习和掌握,你可以充分利用Spring框架的强大功能,提高你的开发效率和代码质量。
192 20
|
Java Maven Spring
springboot学习一:idea社区版本创建springboot项目的三种方式(第三种为主)
这篇文章介绍了在IntelliJ IDEA社区版中创建Spring Boot项目的三种方法,特别强调了第三种方法的详细步骤。
10965 0
springboot学习一:idea社区版本创建springboot项目的三种方式(第三种为主)
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
286 9
|
数据采集 监控 Java
SpringBoot日志全方位超详细手把手教程,零基础可学习 日志如何配置及SLF4J的使用......
本文是关于SpringBoot日志的详细教程,涵盖日志的定义、用途、SLF4J框架的使用、日志级别、持久化、文件分割及格式配置等内容。
853 2
SpringBoot日志全方位超详细手把手教程,零基础可学习 日志如何配置及SLF4J的使用......
|
存储 开发框架 Java
什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
文章详细介绍了Spring、IOC、DI的概念和关系,解释了控制反转(IOC)和依赖注入(DI)的原理,并提供了IOC的代码示例,阐述了Spring框架作为IOC容器的应用。
862 1
什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
|
前端开发 Java 数据库
SpringBoot学习
【10月更文挑战第7天】Spring学习
183 9
|
XML Java 数据格式
Spring学习
【10月更文挑战第6天】Spring学习
119 1

热门文章

最新文章