一、

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. 实现方法 C# button快捷键

    本文讲解了三种方法实现C# button快捷键,如Alt + *(按钮快捷键),Ctrl+*及其他组合键等. C# button快捷键之第一种:Alt + *(按钮快捷键) 在大家给button.la ...

  2. 编码、解码Html代码

    引用 Base64-80.js 文件 做网页的时候有时候需要把富文本框的html代码保存到数据库,那么就需要编码后保存到数据库.浏览器端或后台再解码作绑定 *编码:encode64(str) *解码: ...

  3. C#微信公众号开发 -- (七)自定义菜单事件之VIEW及网页(OAuth2.0)授权

    通俗来讲VIEW其实就是我们在C#中常用的a标签,可以直接在自定义菜单URL的属性里面写上需要跳转的链接,也即为单纯的跳转. 但更多的情况下,我们是想通过VIEW来进入指定的页面并进行操作. 举一个简 ...

  4. OC与Swift的区别三(条件语句)

    11.swift中的switch结构 区别一: oc中switch条件只可以放整数 swift中switch条件可以放几乎任何数据类型 区别二: oc中每一个case中应有break,如果没有brea ...

  5. javascript DOM操作 第19节

    <html> <head> <title>DOM对象</title> <script type="text/javascript&quo ...

  6. java新手笔记22 接口示例2

    1.USB package com.yfs.javase; public interface USB { //定义规范 public void read(); public void write(); ...

  7. JQuery AJAX介绍

    new ActiveXObject("Microsoft.XMLHTTP")是IE中创建XMLHttpRequest对象的方法.非IE浏览器中创建方法是new XmlHttpReq ...

  8. swift基本语法

    swift种语法着实怪异,实质干的事情还是一样的,一下将对此语法做简单介绍: 1.swift语法种已经剔除“:”这个结束符号,下面将演示入门操作的hello world import Foundati ...

  9. Jquery操作单选按钮(Radio)的取值赋值实现代码

    1.获取选中值,三种方法都可以: $('input:radio:checked').val(); $("input[type='radio']:checked").val(); $ ...

  10. Android中图片的异步加载

    转: 1.  为什么要异步加载图片 下载图片比较费时,先显示文字部分,让加载图片的过程在后台,以提升用户体验 2.  SoftReference的作用 栈内存—引用 堆内存—对象 Eg: Object ...