上一篇是基于java手动装配bean的实现,这一篇将通过xml手动装配bean来实现。

xml配置相对于java配置有点:

  1. xml配置更加快捷
  2. 但不宜扩展

一、打开application.xml

  1、注释掉<context:component-scan base-package="soundsystem"></context:component-scan>

<?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>-->
<!--
自动化的bean命名方式非常方便,但如果接下来要引用的话就会感觉被约束
所以用id属性为每个bean创建自己的名字
-->
<bean id="cd" class="soundsystem.SgtPeppers"></bean> <bean id="cdPlayer" class="soundsystem.CDPlayer">
<constructor-arg ref="cd"></constructor-arg>
</bean>
</beans>

二、测试代码

  

package test;

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

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的三种方式

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

  8. Spring实战(三)Spring中装配Bean的三种方式---XML、JavaConfig、AutoWire

    创建应用对象之间协作关系的行为称为装配(wiring),这也是依赖注入的本质. Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系,而开发者需要告诉Spring需要创建哪些 ...

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

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

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

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

随机推荐

  1. bash shell最基本的语法

    1 shell语句的基本构成 shell每个基本的构成元素之间都相隔一个空格. 比如[ -e file ],[.-e.file.]这四个基本元素之间都相隔了一个空格. 同样的道理[ ! -e file ...

  2. Android开发之开机自动启动应用

    package com.raycloud.wolf.autostart; import android.content.BroadcastReceiver; import android.conten ...

  3. strong and weak 强引用和弱引用的差别

    (weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会被释放  ,即使还有weak型指针指向它. 一旦最后一个strong型指针离去 .这个对象将被释放,全部剩余 ...

  4. ros使用时的注意事项&技巧2

    1.查看参数列表 rosparam list 2.查询参数rosparam get parameter_name,如rosparam get /rosdistro 3.设置参数rosparam set ...

  5. 取clientdataset detal中的 更新数据, 将detal 转 数据库脚本sql

    转自永南博客,更改update 脚本只取变化字段,更改排除blob与数组字段,这两个类型会报错 function vartosql(value: Variant): wideString; var  ...

  6. 设置sublime text2/3中预览浏览器快捷键的方法

    我们为什么要设置默认的预览浏览器呢?因为搞前端的都知道,你在预览的时候不可能只预览一个浏览器,可能需要多个,当然今天我们举例中会说道谷歌浏览器Chrome,IE浏览器Internet Explorer ...

  7. html5--6-16 CSS3中的文字与字体

    html5--6-16 CSS3中的文字与字体 中文字体包很大,少量字体的话可以有其它方法. 有字库-首页-全球第一中文web font(在线字体)服务平台.web font.webfont.在线字体 ...

  8. hdu 4398 Template Library Management(贪心+stl)

    题意:n道题,每道题需要一个模板,现在手头有m个模板(标号1~m),解题的时候,如果没有需要的模板,可以向朋友借,但是用完之后必须在还给朋友一个模板(也就是说保持手头拥有m个模板),求解完n道题最少需 ...

  9. 并不对劲的bzoj5475:loj2983:p5206:[wc2019]数树

    题目大意 task0:有两棵\(n\)(n\leq10^5)个点的树\(T1,T2\),每个点的点权可以是一个在\([1,y]\)里的数,如果两个点既在\(T1\)中有直接连边,又在\(T2\)中有直 ...

  10. SPOJ:Another Version of Inversion(二维数组的逆序对)

    DCE Coders admins are way much geekier than they actually seem! Kartik has been following that tradi ...