一、

1.

 package chapter01.sia.knights.config;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import chapter01.sia.knights.BraveKnight;
import chapter01.sia.knights.Knight;
import chapter01.sia.knights.Quest;
import chapter01.sia.knights.SlayDragonQuest; @Configuration
public class KnightConfig { @Bean
public Knight knight() {
return new BraveKnight(quest());
} @Bean
public Quest quest() {
return new SlayDragonQuest(System.out);
} }

解析:

(1)public Knight knight() {

return new BraveKnight(quest());
}  说明new knight时用BraveKnight

(2)@Bean

public Quest quest() {
return new SlayDragonQuest(System.out);
}  说明new quest时用SlayDragonQuest

 package chapter01.sia.knights;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class KnightMain { public static void main(String[] args) throws Exception {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:knight.xml");
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:minstrel.xml");
ApplicationContext context = new AnnotationConfigApplicationContext(
chapter01.sia.knights.config.KnightConfig.class);
Knight knight = context.getBean(Knight.class);
knight.embarkOnQuest();
//context.close();
} }

运行

Embarking on quest to slay the dragon!

SPRING IN ACTION 第4版笔记-第一章-004-用类来管理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版笔记-第一章-002-DI介绍

    一. 1.knight.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  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. verilog语法:进程名

    always @ (posedge clk, negedge rstn)begin:State_flipflops if(!rstn) y<=S1; else y<=Y;end请问这段程序 ...

  2. ThinkPHP函数详解:A方法

    A方法用于在内部实例化控制器,调用格式:A('[项目://][分组/]模块','控制器层名称')最简单的用法: $User = A('User'); 复制代码 表示实例化当前项目的UserAction ...

  3. Unity3D 之防止刚体碰撞导致旋转

    有时候两个刚体发生碰撞的时候,其中一个质量小的会有发生旋转的情况 如果遇到这样的情况,只需要给刚体添加一个约束就行了. 添加这个就行了.

  4. 调试环境部署续:vs远程调试

    原文http://www.bitscn.com/weixin/464994.html 第一步  IIS的配置 进入iis,点击网址,选择你的网站,在窗口的右边编辑网站中点击绑定,如图所示. 进入网站绑 ...

  5. 20160418javaweb之 Filter过滤器

    Servlet规范中 Servlet Listener Filter 1.开发Filter 想要开发一个过滤器需要如下两个步骤: (1)写一个类实现特定的接口Filter 生命周期:当服务器启动时,w ...

  6. php安全模式

    http://www.cnblogs.com/samson/archive/2011/08/08/2130550.html php安全模式:safe_mode=on|off启用safe_mode指令将 ...

  7. OC与Swift的区别二(常量、变量、运算符)

    4.常量与变量声明 oc的变量声明使用  类型 变量名 = 变量值的方式,其中类型为系统内置数据类型或自定义类型,变量名需由英文字母开头且不能包含特殊字符 swift变量声明使用 var 变量名 = ...

  8. Spring中Bean实例的生命周期及其行为

  9. mkisofs出错解决办法

    使用mkisofs遇到错误: genisoimage: Uh oh, I cant find the boot catalog directory 'beini/boot/isolinux'! 使用的 ...

  10. (hdu)1160 FatMouse's Speed

    Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...