java-spring-1

2. (装)配 (B)ean

声明 Bean

构造器注入

Setter 方法注入

装配 bean

控制 Bean 的创建和销毁

装配 创建应用对象之间协作关系

显式:xml 配置,java 配置

隐式:自动装配机制

自动装配 Bean

组件扫描 Component Scanning

Spring 自动发现 AC 中创建的 Bean

自动装配 Autowiring

Spring 自动满足 Bean 之间的依赖

样例

定义接口,降低耦合

1
2
3
public interface CompactDisc {
void play();
}

提供一个接口的实现

@Component 注解,告知 Spring 为它创建 Bean

@Component("lonelyHeartsClub") 可以给 Bean 命名

1
2
3
4
5
6
7
8
9
@Component
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);
}
}

@Configuration @ComponentScan 启用组件扫描

1
2
3
4
5
@Configuration
@ComponentScan
public class CDPlayerConfig {

}

测一把

1
2
3
4
5
6
7
8
9
10
11
@RunWith(SPringJunit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CompactDisc cd;

@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}

@Autowired 可以用在构造器和 Setter 上

当 Spring 创建另一个 Bean 的时候,会通过这个构造器实例化,传过去一个可以设置的 Bean

test-day-161

Today’s Task

  • Hadar Jenkins Server Selector
  • Hadar CI Support
  • Docker remove local image script

Additional Task

Thought

中午稍微看了会 Spring in actrion

第四版确实比第五版多了好多基础概念的讲解……

java-spring-0

Spring in Action Ver.4

目标:撸一个基础的传统 Spring 项目,也许大概花 2~3 周左右?一天一章

4 和 5 对照着看可能会有更多收获

4 有更多基础概念,但版本会老一些

示例项目也并不同 (又可以水一个新项目了好耶

Spring:简化开发的某个开源框架

Bean: 约等于 POJO

POJO: Plain Old Java Ojbect

DI: Dependency Injection 依赖注入

AOP: Aspect Oriented Programming 面向切面编程

AC: Application Context 应用上下文

1 Spring 之旅

策略

  1. POJO, 轻量级,最小侵入性编程
  2. DI与面向接口,松耦合
  3. 切面,惯例,声明式
  4. 切面,模板,减少样板式代码

DI

好处:松耦合

如何实现?

方式一: 构造器注入 Constructor Injection

把对象塞进构造方法的参数里面传过去,而不是在构造器里创建一个新的对象

装配:创建应用组件之间协作的行为

可以通过 xml 或者 java 描述配置

两者等价

1
2
3
4
5
6
7
8
9
<beans>
<bean id="knight" class="com.springinaction.knights.BraveKnight">
<constructor-arg ref="quest" />
</bean>

<bean id="quest" class="com.springinaction.knights.SlayDragonQuest">
<constructor-arg value="#{T(System).out}" />
</bean>
<beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class KnightConfig {
@Bean
public Knight knight() {
return new BraveKnight(quest());
}

@Bean
public Quest quest() {
return new SlayDragonQuest(System.out);
}
}

具体咋实现的还得去看八股文

先会用再说

有需要的话依赖注入可以详细去看 《Dependency Injection》 Dhanji R. Prasanna

AC 用来负责对象的创建和组装

AOP

分离功能形成可重用组件

1
2
3
4
5
6
7
8
9
10
11
12
<beans>
<aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut id="embark"
expression="execution(* *.embarkOnQuest(..))" />
<aop:before pointcut-ref="embark"
method="singBeforeQuest" />
<aop:after pointcut-ref="embark"
method="singAfterQuest" />
</aop:aspect>
</aop:config>
</beans>

样板式代码

比如说 jdbc 那一堆创建链接,捕捉异常,打扫战场的一套

Bean

Spring Container: 创建对象,装配,配置,管理生命周期

Spring 容器的实现:bean factory 或者 Applictaion Context

常用的一些 AC

1
2
3
4
5
AnnotationConfigApplicationContext
AnnotationConfigWebApplicationContext
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
XmlWebApplicationContext

Bean Lifecycle

Spring bean 实例化

填充属性

BeanNameAware.setBeanName()

BeanFactoryAware.setBeanFactory()

AppplicationContextAware.setApplicationContext()

BeanPostProcessor.postProcessBeforeInitialization()

InitializingBean.afterPropertiesSet()

BeanPostProcessor.postProcessAfterInitialization()

准备就绪,可以用

容器关闭

DisposableBean.destroy()

test-day-160

Today’s Task

  • Hadar Jenkins selector
  • Hadar CI Support

Additional Task

Thought

想办法在晚上十一点前解决完每日的所有执念

晚上睡好了的话午休就只需要二十分钟

早晨地铁肝原神,午休肝 15213 Lab,地铁上阅读,骑行台运动一小时,Spring Boot 项目

现在属于是基础也想补,应用框架那边也想撸

test-day-152

Today’s Task

  • Audit V4 more accurate
  • 2 pending dockerfile mrs
  • liquibase pipeline nucleus fix

Additional Task

Thought

先把手头上的事情踏实做好吧

test-day-151

Today’s Task

  • Catalog Clear DB Pipelines
  • x509 temporary fix on groovy

Additional Task

  • Check idgen image with Carlos
  • Confirm Nucleus image version with tty
  • Build nexus 78+312 image for onebox
  • Push customizedlog image to AWS

Thought

回到办公室啦

中间关在家里 WFH 的时候就没有去算太多日期

干脆就和之前连起来了

和自家书房 setup 相比少了块屏幕

干活效率反而直线上升

键盘的白噪声,宽敞明亮的办公桌,也不用担心掉线

还有 RealForce

这下到家又会不习惯 hhkb 了哈哈哈哈

csapp-02.md

02. BOMB LAB

notes and progress

objdump -t

打印出 bomb 的 symbol table

里面会有所有用到的函数名以及全局变量

还有他们的地址

objdump -d

把 bomb 拆成汇编代码

里面有用到的各个函数,慢慢刚汇编吧

strings bomb

会列出所有 bomb 用到的 string

里面有一行画风清奇

因为太长所以只截了人类容易阅读的一部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
➜  bomb git:(master) ✗ strings bomb

That's number 2. Keep going!
Halfway there!
Good work! On to the next...
Welcome to my fiendish little bomb. You have 6 phases with
which to blow yourself up. Have a nice day!
Phase 1 defused. How about the next one?
So you got that one. Try this one.
Border relations with Canada have never been better.
Wow! You've defused the secret stage!
flyers
maduiersnfotvbylSo you think you can stop the bomb with ctrl-c, do you?
Curses, you've found the secret phase!
But finding it and solving it are quite different...
Congratulations! You've defused the bomb!
Well...
OK. :-)
Invalid phase%s
BOOM!!!
The bomb has blown up.
%d %d %d %d %d %d
Error: Premature EOF on stdin
GRADE_BOMB
Error: Input line too long
%d %d %s
DrEvil
greatwhite.ics.cs.cmu.edu
angelshark.ics.cs.cmu.edu
makoshark.ics.cs.cmu.edu
Program timed out after %d seconds
Error: HTTP request failed with error %d: %s
GET /%s/submitr.pl/?userid=%s&lab=%s&result=%s&submit=submit HTTP/1.0
Error: Unable to connect to server %s
%%%02X
%s %d %[a-zA-z ]
changeme.ics.cs.cmu.edu
AUTORESULT_STRING=%s
csapp

那个就是 phase 1

Border relations with Canada have never been better.

Phase 2

开始刚汇编吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
0000000000400efc <phase_2>:
400efc: 55 push %rbp
400efd: 53 push %rbx
400efe: 48 83 ec 28 sub $0x28,%rsp
400f02: 48 89 e6 mov %rsp,%rsi
400f05: e8 52 05 00 00 call 40145c <read_six_numbers>
400f0a: 83 3c 24 01 cmpl $0x1,(%rsp)
400f0e: 74 20 je 400f30 <phase_2+0x34>
400f10: e8 25 05 00 00 call 40143a <explode_bomb>
400f15: eb 19 jmp 400f30 <phase_2+0x34>
400f17: 8b 43 fc mov -0x4(%rbx),%eax
400f1a: 01 c0 add %eax,%eax
400f1c: 39 03 cmp %eax,(%rbx)
400f1e: 74 05 je 400f25 <phase_2+0x29>
400f20: e8 15 05 00 00 call 40143a <explode_bomb>
400f25: 48 83 c3 04 add $0x4,%rbx
400f29: 48 39 eb cmp %rbp,%rbx
400f2c: 75 e9 jne 400f17 <phase_2+0x1b>
400f2e: eb 0c jmp 400f3c <phase_2+0x40>
400f30: 48 8d 5c 24 04 lea 0x4(%rsp),%rbx
400f35: 48 8d 6c 24 18 lea 0x18(%rsp),%rbp
400f3a: eb db jmp 400f17 <phase_2+0x1b>
400f3c: 48 83 c4 28 add $0x28,%rsp
400f40: 5b pop %rbx
400f41: 5d pop %rbp
400f42: c3 ret

TO BE CONTINUED…

csapp-lec-05.md

Preface

想不起太多骑行在路上时的心里想法

反正就是想去看底层浪漫网课了

想看就去做作业了

四月的时候也重新开始过,碰到周末实在是难继续

迷茫啊

每天对着脚本都快 Burn Out 了

可能最近已经经历过一轮了吧

工作效率直线下降

与其浪费时间在纠结方向上,不如都去试试

反正就是先做起来了,这回能撑多久不知道

这回没有学分,没有GPA,没有DDL,没有自己在做与众不同的事情的蜜汁快感,对未来就业没有任何直接帮助

这甚至是大一的基础课……

我甚至当年还看过……

科班出身毕业两年的人要回头去看这些东西说出来甚至是耻辱好吧

但我现在完全不会

不会就是不会

那就读个档吧

Lecture 05 Machine level Programming I Basics

assembly code == machine code

what the machine is trying to do

the core of the course

CISC Complex Instruction Set Computers

RISC Reduced Instruction Set Computers

RISC vs. CISC in 80s

C, assembly, machine code

Architecture (ISA): compiler target, parts of processor design to write assembly code

Microarchitecture: implementation of the architecture

Program counter:

Address of next instruction

Registers

program data, heavily used

Condition codes:

store status information

Memory:

byte addressable array

code, user data

C program — Compiler —> ASM program — Assembler —> Object program — Linker —> Exceutable program

gcc -Og -S sum.c

-S, stop at
-Og, optimization at level g for debugging in recent versions of gcc
-O1, turn on the optimizer in the past, be more efficient

disassembler

objdump -d sum

referse engineers from object code to assembly code

var/func names are all lost at this assembly level, all of them are just machine code

Assembly

only do one thing per instruction

transfer data between memory and register

arithmetic functions

control flow

gdb can also disassemble function

Assembly basics: registers, operands, move

registers

%rxx, 64 bit

%exx, 32 bit, lower 32 bits of the 64 bit stuff

1
2
3
4
5
6
%rax, %rbx, %rcx, %rdx

%rsi, %rdi, %rsp, %rbp

%r8, %r9 ... %r15

rx-abcd

r8…r15

rsi, rdi

rsp, rbp

legacy purpose:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
accumulate

base

counter

data

si: source index

di: destination index

sp: stack pointer

bp: base pointer

move

moving data

1
movq source dest

CANNOT MOVE value from MEMORY TO MEMORY

read value from memory

copy it to register

write it to memory

1
2
3
4
5
6
D(Rb, Ri, S)

MEM[MEM(Rb) + S * MRM(Ri) + D]

naturlly work with arrays

q mean 8 bytes

bwlq, 1248

Arithmetic & logical operations

Address Computation Instruction

leaq Src, Dst

load effective address

compute address without a memory reference

compute x + k * y, k = 1, 2, 4, 8

addq, subq, imulq

salq: dest = dest << src

sarq: dest = dest >> src, arithmetic, add sign in the left

shrq: des = dest >> src, logical, add 0 in the left

xorq

andq

orq

src comes in the first

destination is the second