Spring------自动化装配Bean(三)
上一篇是基于java手动装配bean的实现,这一篇将通过xml手动装配bean来实现。
xml配置相对于java配置有点:
- xml配置更加快捷
- 但不宜扩展
一、打开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(三)的更多相关文章
- Spring自动化装配bean
1. 场景 用CD(Compact disc)和CD播放器(CDPlayer)阐述DI(依赖注入). 如果不将CD插入(注入)到CDPlayer中,那么CDPlayer其实没有太大的用处,所以,可以这 ...
- Spring 之自动化装配 bean 尝试
[Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...
- 第2章—装配Bean—自动化装配Bean
自动化装配Bean 2.1.Spring配置可选方案 装配是依赖注入DI的本质,Spring提供了以下三种注入的装配机制: 在XMl中进行显式配置 在java中进行显式配置 隐式的Bean发现机制 ...
- 二、spring中装配bean
在spring框架中提供了三种 bean的装配方式,当然这三种装配方式是可以灵活的进行组合使用的,项目中使用最多的是自动装配bean的方式,也就是通过注解的方式进行bean的装配,一下是四种装配方式的 ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring容器装配Bean的三种方式
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...
- Spring实战(三)Spring中装配Bean的三种方式---XML、JavaConfig、AutoWire
创建应用对象之间协作关系的行为称为装配(wiring),这也是依赖注入的本质. Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系,而开发者需要告诉Spring需要创建哪些 ...
- Spring学习系列(二) 自动化装配Bean
一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...
- Spring入门(二):自动化装配bean
Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...
随机推荐
- iframe引入页面
将外层css框架单独存放在一个css文件之中,将iframe的css单独写在一个css文件,避免iframe中的html,body等公共部分的样式冲突. 外层框架单独写在一个css,如: frame. ...
- 更改scroll样式
/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 2px; height: 80%; background: #fff; } /*定 ...
- Could not load file or assembly 'MyAssembly.XmlSerializers
https://stackoverflow.com/questions/17755559/could-not-load-file-or-assembly-myassembly-xmlserialize ...
- codeforces 414B B. Mashmokh and ACM(dp)
题目链接: B. Mashmokh and ACM time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 并不对劲的多项式求ln,exp
ln 解释 设\(g(x)=ln(f(x))\),两边同时求导,则有:\(g'(x)=ln'(f(x))*f'(x)=f^{-1}(x)*f'(x)\)(1) 因为\(f(x)\)是个多项式,所以设\ ...
- iOS成员变量、实例变量、属性变量三者的联系与区别
一.类Class中的属性property 在ios第一版中: 我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: 注意:(这个是 ...
- Dijkstra再理解+最短路计数
众所周知,Dijkstra算法是跑单源最短路的一种优秀算法,不过他的缺点在于难以处理负权边. 但是由于在今年的NOI赛场上SPFA那啥了(嗯就是那啥了),所以我们还是好好研究一下Dij的原理和它的优化 ...
- Bootstrap-CSS:排版
ylbtech-Bootstrap-CSS:排版 1.返回顶部 1. Bootstrap 排版 Bootstrap 使用 Helvetica Neue. Helvetica. Arial 和 sans ...
- web_html-day1
概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...
- NOI前总结:点分治
点分治: 点分治的题目基本一样,都是路径计数. 其复杂度的保证是依靠 $O(n)$ 找重心的,每一次至少将问题规模减小为原先的$1/2$. 找重心我喜欢$BFS$防止爆栈. int Root(int ...