首先创建项目

pom.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.geyao</groupId><artifactId>spring01geyao</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.13.RELEASE</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies>
</project>

配置log4j的配置文件

### 设置###
log4j.rootLogger = INFO,stdout### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.category.org.springframework.beans.factory=DEBUG

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;public CDPlayer() {super();System.out.println("CDPlayer无参构造方法");}
@Autowiredpublic CDPlayer(CompactDisc cd) {this.cd = cd;System.out.println("CDPlayer有参构造方法");}public void play(){cd.play();}
}

App类

package soundSystem;import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;@ComponentScan
public class App {public static void main(String[] args){ApplicationContext context=new AnnotationConfigApplicationContext(App.class);CDPlayer player=context.getBean(CDPlayer.class);player.play();}
}

运行结果

[INFO ] 2019-10-29 18:56:54,850 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583)
Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@880ec60: startup date [Tue Oct 29 18:56:54 CST 2019]; root of context hierarchy
[DEBUG] 2019-10-29 18:56:54,874 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 18:56:54,875 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
[DEBUG] 2019-10-29 18:56:54,900 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 18:56:54,904 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 18:56:54,995 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 18:56:54,996 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
[DEBUG] 2019-10-29 18:56:54,997 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 18:56:55,030 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 18:56:55,030 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 18:56:55,031 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
[DEBUG] 2019-10-29 18:56:55,032 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 18:56:55,040 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 18:56:55,046 method:org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:730)
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@166fa74d: 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,app,CDPlayer,compactDisc]; root of factory hierarchy
[DEBUG] 2019-10-29 18:56:55,047 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 18:56:55,047 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 18:56:55,050 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 18:56:55,050 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 18:56:55,051 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
[DEBUG] 2019-10-29 18:56:55,059 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 18:56:55,063 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 18:56:55,064 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 18:56:55,064 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
[DEBUG] 2019-10-29 18:56:55,065 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 18:56:55,070 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 18:56:55,072 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'app'
[DEBUG] 2019-10-29 18:56:55,072 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'app'
[DEBUG] 2019-10-29 18:56:55,073 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
Eagerly caching bean 'app' to allow for resolving potential circular references
[DEBUG] 2019-10-29 18:56:55,076 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'app'
[DEBUG] 2019-10-29 18:56:55,076 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'CDPlayer'
[DEBUG] 2019-10-29 18:56:55,077 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'CDPlayer'
[DEBUG] 2019-10-29 18:56:55,120 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'compactDisc'
[DEBUG] 2019-10-29 18:56:55,121 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'compactDisc'
compactdisc无参构造方法
[DEBUG] 2019-10-29 18:56:55,123 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 18:56:55,126 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'compactDisc'
[DEBUG] 2019-10-29 18:56:55,127 method:org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:757)
Autowiring by type from bean name 'CDPlayer' via constructor to bean named 'compactDisc'
CDPlayer有参构造方法
[DEBUG] 2019-10-29 18:56:55,129 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 18:56:55,131 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'CDPlayer'
[DEBUG] 2019-10-29 18:56:55,131 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'compactDisc'
[DEBUG] 2019-10-29 18:56:55,132 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 18:56:55,194 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'lifecycleProcessor'
[DEBUG] 2019-10-29 18:56:55,199 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'CDPlayer'
正在播放音乐....

spring学习(10):创建项目(自动装配)相关推荐

  1. Spring学习(五)—— 自动装配

    自动装配 搭建环境 首先,搭建环境 Cat.java package com.zhang.bean;/*** @title: Cat* @Author 张宜强* @Date: 2021/7/22 11 ...

  2. spring学习(48):自动装配中定义的bean的作用域

    目录结构 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  3. Spring注解驱动开发学习总结8:自动装配注解@Autowire、@Resource、@Inject

    Spring注解驱动开发学习总结8:自动装配注解@Autowire.@Resource.@Inject 1.自动装配@Autowire.@Resource.@Inject 1.1 构建bookDao ...

  4. SpringBoot2.X (2)- 使用Spring Initializer 快速创建项目

    SpringBoot - 使用Spring Initializer 快速创建项目 ① New Project 这里不选择Maven,选择Spring Initializer. ② 编写项目类型.项目名 ...

  5. Spring学习(七)bean装配详解之 【通过注解装配 Bean】【自动装配的歧义解决】...

    本文借鉴:Spring学习,@Bean 的用法(特此感谢!) 自动装配 1.歧义性 我们知道用@Autowired可以对bean进行注入(按照type注入),但如果有两个相同类型的bean在IOC容器 ...

  6. Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】

    本文借鉴:Spring学习(特此感谢!) 通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XM ...

  7. Spring学习(五)bean装配详解之 【XML方式配置】

    本文借鉴:Spring学习(特此感谢!) 一.配置Bean的方式及选择 配置方式 在 XML 文件中显式配置 在 Java 的接口和类中实现配置 隐式 Bean 的发现机制和自动装配原则 方式选择的原 ...

  8. Spring容器父子类继承关系交给spring容器管理采用@autowired自动装配分析

    第一个青春是上帝给的 第二个的青春是靠自己努力的. 在开发项目过程中开发者们可能会遇到这样的问题 @autowired自动装配模式的工作模式是:在springIOC定位所有的Bean后,这个字段需要按 ...

  9. Spring实战(六)自动装配的歧义性

    1.Spring进行自动装配时碰到的bean歧义性问题. 在进行自动装配时,只有仅有一个bean匹配所需结果时,才是可行的. 如果不仅仅一个bean能够匹配结果,例如一个接口有多个实现,这种歧义性会阻 ...

  10. 【Spring Boot】条件化自动装配

    [引言] 在前面的博客中,了解了Spring Boot的自动装配是如何实现的,这只是一个基础,对于自动装配,更专业的使用是可以做到条件化自动装配. [条件注解] Spring Boot中,提供了以下条 ...

最新文章

  1. Vue开源项目库汇总
  2. 【开发环境】Mac 中删除 Python ( 删除 Python 框架 | 删除 Python 应用程序 | 删除 Python 链接 )
  3. 从零开始的51单片机——VsCode+EIDE环境搭建
  4. 七、FFmpeg使用---X264静态库编入FFmpeg
  5. win10系统使用MarkdownPad2无法渲染
  6. linux修改控制台分辨率,linux控制台分辨率
  7. 遇到NOD32更新时提示“用户名和密码无效”解决办法
  8. Jmeter性能测试面试基础问答
  9. 阿里金融云操作教程Plus
  10. poj 3295 Tautology【离散数学之重言式】
  11. 适合练习听力的英文电影推荐
  12. sql注入进阶/user-agent/基于报错的注入/保姆级教程/一看就会/
  13. Formality总结
  14. 智慧运维解决方案-最新全套文件
  15. 注销苹果id 显示联系服务器时出现问题,帮您还原连接appleid服务器时出错 【图文介绍】的处理办法_...
  16. java支付宝rsa2签名,使用支付宝SDK进行RSAj加签验签以及加密解密
  17. latex参考文献中输入
  18. TCP/IP高频考点之一个数据包的流浪日记 - 网络层
  19. Unity 为游戏对象设置标签
  20. 【逻辑】500桶酒,其中1桶是毒酒,找毒酒

热门文章

  1. 技术面试官的一些建议
  2. mysql 从服务器同步设置_mysql主从同步配置
  3. 两点定标法_一种两点校正红外热像仪的非均匀性的模块及方法
  4. JAVA实现在面板中添加图表_Java 创建PowerPoint图表并为其添加趋势线
  5. php 判断页面加载完,所有ajax执行完且页面加载完判断
  6. 12306订票助手java_12306订票助手
  7. 获取异常信息_如何在 ASP.NET Core 中实现全局异常拦截
  8. 通过WordPress内置函数批量添加文章
  9. Linux 驱动开发之内核模块开发 (二)—— 内核模块编译 Makefile 入门
  10. Libusb开发教程三 USB设备程序开发