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
相关文章
|
24天前
|
Java 开发者 Spring
Spring Framework 中的 @Autowired 注解:概念与使用方法
【4月更文挑战第20天】在Spring Framework中,@Autowired 注解是实现依赖注入(Dependency Injection, DI)的一种非常强大的工具。通过使用 @Autowired,开发者可以减少代码中的引用绑定,提高模块间的解耦能力
33 6
|
3月前
|
安全 Java Spring
【Spring Boot 源码学习】HttpEncodingAutoConfiguration 详解
本篇带大家一起从源码了解 Spring Boot 内置的Http编码功能
66 8
【Spring Boot 源码学习】HttpEncodingAutoConfiguration 详解
|
4月前
|
Java 应用服务中间件 Spring
【Spring Boot 源码学习】@Conditional 条件注解
【1月更文挑战第8天】本篇介绍 @Conditional 条件注解及其衍生注解
114 3
【Spring Boot 源码学习】@Conditional 条件注解
|
2月前
|
Java Spring 容器
Spring中@Autowired和@Resource注解异同点
Spring中@Autowired和@Resource注解异同点
35 0
|
2月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
197 0
|
6天前
|
存储 前端开发 Java
Spring Boot自动装配的源码学习
【4月更文挑战第8天】Spring Boot自动装配是其核心机制之一,其设计目标是在应用程序启动时,自动配置所需的各种组件,使得应用程序的开发和部署变得更加简单和高效。下面是关于Spring Boot自动装配的源码学习知识点及实战。
13 1
|
15天前
|
XML Java 数据格式
Spring学习__一篇足矣
Spring学习__一篇足矣
Spring学习__一篇足矣
|
19天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
20天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
20天前
|
Dubbo Java 应用服务中间件
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架