一、

1.knight.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="knight" class="chapter01.sia.knights.BraveKnight">
<constructor-arg ref="quest" />
</bean> <bean id="quest" class="chapter01.sia.knights.SlayDragonQuest">
<constructor-arg value="#{T(System).out}" />
</bean> </beans>

2.Knight

 package chapter01.sia.knights;

 public interface Knight {

   void embarkOnQuest();

 }

3.BraveKnight

 package chapter01.sia.knights;

 public class BraveKnight implements Knight {

   private Quest quest;

   public BraveKnight(Quest quest) {
this.quest = quest;
} public void embarkOnQuest() {
quest.embark();
} }

4.SlayDragonQuest

 package chapter01.sia.knights;

 import java.io.PrintStream;

 public class SlayDragonQuest implements Quest {

   private PrintStream stream;

   public SlayDragonQuest(PrintStream stream) {
this.stream = stream;
} public void embark() {
stream.println("Embarking on quest to slay the dragon!");
} }

5.

 package chapter01.sia.knights;

 import org.springframework.context.support.
ClassPathXmlApplicationContext; public class KnightMain { public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:knight.xml");
Knight knight = context.getBean(Knight.class);
knight.embarkOnQuest();
context.close();
} }

6.运行

 三月 01, 2016 9:42:47 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ec12ad8: startup date [Tue Mar 01 09:42:47 CST 2016]; root of context hierarchy
三月 01, 2016 9:42:47 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [knight.xml]
三月 01, 2016 9:42:47 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4ec12ad8: startup date [Tue Mar 01 09:42:47 CST 2016]; root of context hierarchy
Embarking on quest to slay the dragon!

SPRING IN ACTION 第4版笔记-第一章-002-DI介绍的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第一章-005-Bean的生命周期

    一. 1. As you can see, a bean factory performs several setup steps before a bean is ready touse. Let’ ...

  2. SPRING IN ACTION 第4版笔记-第一章-003-AOP介绍

    一.目标 要在BraveKnight调用embarkOnQuest()前后各做一些处理(调用Minstrel的方法) 二. 1.minstrel.xml <?xml version=" ...

  3. Spring In Action 第4版笔记-第一章-001架构

    1.Spring’s fundamental mission: Spring simplifies Java development. 2.To back up its attack on Java ...

  4. SPRING IN ACTION 第4版笔记-第一章-004-用类来管理DI

    一. 1. package chapter01.sia.knights.config; import org.springframework.context.annotation.Bean; impo ...

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

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

  6. SPRING IN ACTION 第4版笔记-第九章Securing web applications-010-拦截请求

    一. What if you wanted to restrict access to certain roles only on Tuesday? Using the access() method ...

  7. SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)

    一. 1.定义接口 Suppose that you need to authenticate against users in a non-relational database suchas Mo ...

  8. SPRING IN ACTION 第4版笔记-第九章Securing web applications-007-设置LDAP server比较密码(contextSource、root()、ldif()、)

    一.LDAP server在哪 By default, Spring Security’s LDAP authentication assumes that the LDAP server is li ...

  9. SPRING IN ACTION 第4版笔记-第九章Securing web applications-004-对密码加密passwordEncoder

    一. 1.Focusing on the authentication query, you can see that user passwords are expected to be stored ...

随机推荐

  1. JUnit4 笔记

    1. JUnit4基础 JUnit4 与 JUnit3不同的是,不需要继承测试类,而是通过JDK5提供的注解去标识测试方法. 常用的注解如下: @Before:初始化方法 对于每一个测试方法都要执行一 ...

  2. 学点css之经验总结篇章

    学css说起来应该有三天左右的时间的,加上之前了解的基础,对css有一点的感性认识了,相应代码有有比较好的把握,现在就通过分享几张照片的形式分享一下我的收获 备注:在Border的外边的部门被称作:o ...

  3. WORDPRESS 后台500错误解决方法集合

    引自: http://www.guuglc.com/565.html 这篇文章本质上我是不可能会写到,就因为7号那天晚上,我准备搬家的时候,发现前台完好,进入后台却500错误. 这时我就得急的,毕竟明 ...

  4. redis基本数据类型【2】-Hash类型

    一.概述 1.散列是一种典型的字典结构,filed和value的映射,但value只能存储字符串,不支持其他类型 2.一个散列类型最多包含 2^32 -1个字段 3.散列适合存储对象:使用对象和ID构 ...

  5. 新建DragonBones动画文件

    本篇文章由:http://www.sollyu.com/new-dragonbones-animation-file/ 说明 我在网上找了很久都没找到关于怎么创建一个DragonBones动画的文章, ...

  6. 浅析JAVA设计模式(三)

    4.接口隔离原则: ISP(Interface Segregation Principle)  客户端不应该依赖它不需要的接口,或者说类的依赖的关系应该建立在最小的接口上.举个例子,直接上代码:  1 ...

  7. RM-Linux驱动--Watch Dog Timer(看门狗)驱动分析

    from:http://blog.csdn.net/geekcome/article/details/6595265 硬件平台:FL2440 内核版本:2.6.28 主机平台:Ubuntu 11,04 ...

  8. 一个开源的可视化的jQuery工作流插件

    特点 1.跨浏览器,可兼容IE7--IE11, FireFox, Chrome, Opera等几大内核的浏览器,且不需要浏览器再加装任何控件. (IE7-IE8时,使用VML:IE9以上,FF,OPE ...

  9. 使用localstorage及js模版引擎 开发 m站设想

    目前 m站开发的方式,依然请求完整的html,这样造成的问题就是每次请求的数据量过大过多,在没有wifi的情况下,导致页面打开的速度很慢,耗费的流量也较多:访问m站的多是移动端设备,其浏览器的版本都较 ...

  10. C#基础(一)——C#中反斜杠/n与/r的区别

    最近在公司实习的过程中,遇到了字符串换行的问题,百度了一下,发现字符串换行的问题还挺多,总结一下最基本的点,以防忘记. \n—>换行符(New Line),作用为换行符后面的字符串显示到“下一行 ...