SPRING IN ACTION 第4版笔记-第一章-004-用类来管理DI
一、
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的更多相关文章
- 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’ ...
- SPRING IN ACTION 第4版笔记-第一章-003-AOP介绍
一.目标 要在BraveKnight调用embarkOnQuest()前后各做一些处理(调用Minstrel的方法) 二. 1.minstrel.xml <?xml version=" ...
- Spring In Action 第4版笔记-第一章-001架构
1.Spring’s fundamental mission: Spring simplifies Java development. 2.To back up its attack on Java ...
- SPRING IN ACTION 第4版笔记-第一章-002-DI介绍
一. 1.knight.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-011-把敏感信息请求转为https(requiresChannel())
1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- sbit命令行中运行scala脚本
一般sbit编译器采成了scala运行工具.启动sbit命令行,输入console,命令行自动切换到scala编辑器面. scala>:paste 然后手动将XXX.scala中的代码拷贝到界面 ...
- 虚拟机及ubuntu环境搭建问题
1.现象: VMware中ubuntu ping通 宿主机windows VMware中ubuntu ping通 百度 宿主机windows ping不通 VMware中ubuntu 解决办法: 确保 ...
- Java List中的一个List选择选择移除方法
记录: 第一个参数:传入需要处理的List 第二个参数:需要处理的参数在List中的标识符 第三个参数:在需要处理的参数中的开始位置 第三个参数:在需要处理的参数中的个数 List<String ...
- Path类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- AndroidManifest.xml中的android:name是否带.的区别
如果android:name所指示的类在定义的package="org.crazyit.ui"下,加不加点无所谓:但如果android:name指示的类在在package下的子包中 ...
- IOS开发中针对UIImageView的几种常用手势
// // ViewController.m // 05-手势 // // Created by wanghy on 15/9/21. // Copyright (c) 2015年 wangh ...
- WPF简单拖拽功能实现
1.拖放操作有两个方面:源和目标. 2.拖放操作通过以下三个步骤进行: ①用户单击元素,并保持鼠标键为按下状态,启动拖放操作. ②用户将鼠标移到其它元素上.如果该元素可接受正在拖动的内容的类型,鼠标指 ...
- LevelDB windows vs2013 c++编译和测试
引用: (src1) :http://download.csdn.net/detail/flyfish1986/8881263(这里有下载地址) (src2) :http://blog.csdn.ne ...
- POJ 2711 Regular Words(DP + 高精度)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1711 题目大意:给定一个正整数n,产生一个3*n位长的串,要求这个串 ...
- (转) IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...