一、创建 CompactDisc接口和SgetPeppers实现类

  CompactDisc接口方法为播放。SgtPeppers实现CompactDisc接口。

 package soundsystem;

 public interface CompactDisc {
void play(); }
 package soundsystem;

 import org.springframework.stereotype.Component;
//component为spring中bean扫描标识
@Component
public class SgtPeppers implements CompactDisc { private String title = "歌德";
private String artist = "gede"; public void play() {
System.out.println("Playing " + title + " by " + artist);
} }

二、启用spring组件扫描

  1、通过java配置启用

    添加 @Configuration @ComponentScan 两个注解即可。

    【注】使用ComponentScan时,若配置文件和bean在同一个包,省略基础包备注也可以。

 package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("soundsystem")
public class JavaConfig { }

  2、通过xml配置启用:<context:component-scan base-package="soundsystem"></context:component-scan>  如果没有自动提示或者报错,在namespace中添加context配置

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="soundsystem"></context:component-scan> </beans>

三、编写测试类,并运行

  1、创建test包,分别通过java配置和xnl配置实现测试

    java配置为:AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(soundsystem.JavaConfig.class);

    xml配置为:ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");

 package test;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import soundsystem.CompactDisc;
import soundsystem.SgtPeppers; public class Test {
public static void main(String[] args) {
//基于java类中配置上下文
//AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(soundsystem.JavaConfig.class);
//基于xml配置上下文
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml"); CompactDisc cd=context.getBean(SgtPeppers.class);
cd.play();
}
}

四、补充说明

  1、spring中设置bean的id有两种方式,一是用户给出,二是系统自己生成默认id(默认将类名首字母变成小写作为bean的id)。

  用户常用指定id方法为:

@Component("id")
//也可以使用named属性
@Named(“id”)

  2、关于组件扫描的基础包,上面提到部分,指定单个基础包,当有多个基础包需要扫描时使用basePackages属性。

 @ComponentScan(basePackages={"soundsystem","test"})

  3、添加@Autowired注解实现自动装配。添加接口MediaPlayer和实现类CDPlayer。修改Test测试类

 package soundsystem;
public interface MediaPlayer {
void play(); }
package soundsystem;

import org.springframework.beans.factory.annotation.Autowired;

public class CDPlayer implements MediaPlayer {
private CompactDisc cd;
@Autowired
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
public void play() {
cd.play();
}
}
 public class Test {
public static void main(String[] args) {
//基于java类中配置上下文
//AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(soundsystem.JavaConfig.class);
//基于xml配置上下文
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
CompactDisc cd=context.getBean(SgtPeppers.class);
cd.play();
MediaPlayer player=context.getBean(CDPlayer.class);
player.play(); }
}

  4、备注CDPlayer重写构造方法,通过Autowired自动添加构造方法CD对象,调用CD播放。

  

Spring------自动化装配Bean(一)的更多相关文章

  1. Spring自动化装配bean

    1. 场景 用CD(Compact disc)和CD播放器(CDPlayer)阐述DI(依赖注入). 如果不将CD插入(注入)到CDPlayer中,那么CDPlayer其实没有太大的用处,所以,可以这 ...

  2. Spring 之自动化装配 bean 尝试

    [Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...

  3. 第2章—装配Bean—自动化装配Bean

    自动化装配Bean 2.1.Spring配置可选方案 ​ 装配是依赖注入DI的本质,Spring提供了以下三种注入的装配机制: 在XMl中进行显式配置 在java中进行显式配置 隐式的Bean发现机制 ...

  4. 二、spring中装配bean

    在spring框架中提供了三种 bean的装配方式,当然这三种装配方式是可以灵活的进行组合使用的,项目中使用最多的是自动装配bean的方式,也就是通过注解的方式进行bean的装配,一下是四种装配方式的 ...

  5. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  6. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  7. Spring学习系列(二) 自动化装配Bean

    一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...

  8. Spring入门(二):自动化装配bean

    Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...

  9. Spring容器装配Bean的三种方式

    欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...

  10. 自动化装配Bean

    一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...

随机推荐

  1. 将线上服务器生成的日志信息实时导入kafka,采用agent和collector分层传输,app的数据通过thrift传给agent,agent通过avro sink将数据发给collector,collector将数据汇集后,发送给kafka

    记flume部署过程中遇到的问题以及解决方法(持续更新) - CSDN博客 https://blog.csdn.net/lijinqi1987/article/details/77449889 现将调 ...

  2. 【POJ 1159】Palindrome

    [POJ 1159]Palindrome 近期各种题各种奇葩思路已经司空见惯了...又新出个滚动数组= = 该题另一点须要知道 最少须要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 ...

  3. Deep Learning 32: 自己写的keras的一个callbacks函数,解决keras中不能在每个epoch实时显示学习速率learning rate的问题

    一.问题: keras中不能在每个epoch实时显示学习速率learning rate,从而方便调试,实际上也是为了调试解决这个问题:Deep Learning 31: 不同版本的keras,对同样的 ...

  4. <转载>调制与解调电路详解

    原文链接:http://www.elecfans.com/analog/20120509270848_4.html 调幅和检波电路 广播和无线电通信是利用调制技术把低频声音信号加到高频信号上发射出去的 ...

  5. HDU5890:Eighty seven(Bitset优化背包)

    Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach ch ...

  6. 非旋treap (BZOJ1895)

    记个板子,还是挺好用的. #include <bits/stdc++.h> using namespace std; ]; int rt,n,m,l,r,x,A,B,C,t; struct ...

  7. GridFS大文件的添加、获取、查看、删除

    GridFS是一种在MongoDB中存储大二进制文件的机制,使用GridFS的原因有以下几种: 存储巨大的文件,比如视频.高清图片等. 利用GridFS可以简化需求. GridFS会直接利用已经建立的 ...

  8. Start Developing Mac Apps -- Mac App Store Mac 应用商店

      Mac App Store The information you’ve read so far focused on how to create an app in Xcode. However ...

  9. ACM-ICPC 中可能会使用到的库

    sort(v.first(),v.end(),cmp())unique(v.first(),v.end(),cmp()) 第三个参数可以传入一个bool型,用来判断是不是相等,返回unique后的超尾 ...

  10. Swift3.0 UITextView写反馈界面

    效果图 适配用的 SnapKit 使用介绍:  http://www.hangge.com/blog/cache/detail_1097.html private func creationTextV ...