1.

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();
} }

2.

 public class SgtPeppers implements CompactDisc {

   private String title = "Sgt. Pepper's Lonely Hearts Club Band";
private String artist = "The Beatles"; public void play() {
System.out.println("Playing " + title + " by " + artist);
} }

3.

 package soundsystem;

 import java.util.List;

 public class BlankDisc implements CompactDisc {

   private String title;
private String artist;
private List<String> tracks; public BlankDisc(String title, String artist, List<String> tracks) {
this.title = title;
this.artist = artist;
this.tracks = tracks;
} public void play() {
System.out.println("Playing " + title + " by " + artist);
for (String track : tracks) {
System.out.println("-Track: " + track);
}
} }

一、在Java配置文件中引入Java配置文件

1.

 package soundsystem;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class CDConfig {
@Bean
public CompactDisc compactDisc() {
return new SgtPeppers();
}
}

2.则在CDPlayerConfig中可直接用@import引入上面的配置或新建一个文件引入这两个配置文件

(1)

 @Configuration
@Import(CDConfig.class)
public class CDPlayerConfig {
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}

(2)

 package soundsystem;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({CDPlayerConfig.class, CDConfig.class})
public class SoundSystemConfig {
}

二、在Java配置文件中引入xml配置文件,可用@ImportResource

1.cd-config.xml

 <?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="compactDisc"
class="soundsystem.BlankDisc"
c:_0="Sgt. Pepper's Lonely Hearts Club Band"
c:_1="The Beatles">
<constructor-arg>
<list>
<value>Sgt. Pepper's Lonely Hearts Club Band</value>
<value>With a Little Help from My Friends</value>
<value>Lucy in the Sky with Diamonds</value>
<value>Getting Better</value>
<value>Fixing a Hole</value>
<!-- ...other tracks omitted for brevity... -->
</list>
</constructor-arg>
</bean> </beans>

2.SoundSystemConfig.java

 package soundsystem;

 import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource; @Configuration
@Import(CDPlayerConfig.class)
@ImportResource("classpath:cd-config.xml")
public class SoundSystemConfig { }

Both beans— CDPlayer configured in JavaConfig and BlankDisc configured in XML —
will be loaded into the Spring container. And because CDPlayer ’s @Bean method
accepts a CompactDisc as a parameter, the BlankDisc bean will be wired into it, even
though it’s configured in XML .

SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入

    一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE 二. 1. package soundsystem.collections; import ...

  2. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>

    一.在xml中引入xml,用<import> <?xml version="1.0" encoding="UTF-8"?> <be ...

  3. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space

    一.注入简单属性 package soundsystem.properties; import org.springframework.beans.factory.annotation.Autowir ...

  4. SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace

    1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...

  5. SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例

    spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean. It appears that the CompactDisc ...

  6. SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法

    1. package soundsystem; import org.springframework.context.annotation.Bean; import org.springframewo ...

  7. SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法

    一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...

  8. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean

    1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...

  9. SPRING IN ACTION 第4版笔记-第九章Securing web applications-011-把敏感信息请求转为https(requiresChannel())

    1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...

随机推荐

  1. hdu 1563 Find your present!

    Find your present! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. [转]内嵌页面iframe以及和其兄弟iframe的相互传值

    原文出处 iframe的调用包括以下几个方面:(调用包含html dom,js全局变量,js方法) 主页面调用iframe: iframe页面调用主页面: 主页面的包含的iframe之间相互调用: 主 ...

  3. 2014年的Google I/O app设计中的材料设计-渣渣的翻译

    又是一篇翻译,用了三个多小时.http://android-developers.blogspot.co.id/2014/08/material-design-in-2014-google-io-ap ...

  4. 几个MVC属性

    1   用于显示提示字符串 [Required(ErrorMessage="请输入类型名称")] public string ArticleTypeName { get; set; ...

  5. 使用sqlmap注入DVWA的SQL Injection菜单

    1 使用sqlmap注入DVWA的SQL Injection菜单 本教程中的登陆地址:http://192.168.0.112/dvwa/login.php 1.1 获取cookie信息 1) 使用a ...

  6. Windows平台下Qt开发环境的搭建

    Qt 是采用开源和商用双协议发布的开放源代码的图形开发类库,现在很多图形化的开源软件都使用了Qt. 下载地址:http://qt-project.org/downloads 1. 下载安装包 你可以从 ...

  7. js 中用Dom2级事件处理函数(改变样式)

    下面这些客户端 javascript代码用到了事件,它给一个很重要的事件--“load" 事件注册了一个事件处理程序.同时展示了注册”click“事件处理函数更高级的一种方法 <!do ...

  8. JavaScript入门介绍(一)

    JavaScript入门介绍 [经常使用的调试工具][w3school.com.cn在线编辑] [Chrome浏览器 开发调试工具]按F121.代码后台输出调试:console.log("t ...

  9. svn命令操作

    非超级用户:sudo 命令 svn info 查看svn地址 svn co 地址 检出仓库

  10. sirius的学习笔记(3)

    毕业论文什么的终于搞完了,重拾我的python Creating the python skeleton project directory $ mkdir project $ cd project ...