spring boot - log4j

Dependency

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- exclude logback-classic -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- add slf4j-log4j12 dependency -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>

Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# root logger setup; [level], appenderName...
log4j.rootLogger=INFO, CL, FILE

# appender; consul
log4j.appender.CL=org.apache.log4j.ConsoleAppender
log4j.appender.CL.layout=org.apache.log4j.PatternLayout
log4j.appender.CL.layout.ConversionPattern=%-5p %c %x - %m%n

# appender; file
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=/tmp/eubalaena.log
log4j.appender.FILE.MaxFileSize=1MB
log4j.appender.FILE.MaxBackupIndex=15
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-5p %c %x - %m%n

log4j.logger.pub.wii=DEBUG, FILE
log4j.logger.org.springframework=INFO, FILE
log4j.logger.org.mybatis=INFO, FILE
org.mybatis.spring.mapper.ClassPathMapperScanner=OFF

java - 正则

match

1
2
3
4
5
6
7
8
9
10
11
12
13
// string.match: yes OR no
private static final String REGEX = "^@encrypt\\{(.*)}$";
"@encrypt{file:mysql.username}".match(REGEX); // return true

// Pattern
String REGEX = "^@encrypt\\{(.*)}$";
Pattern PT = Pattern.compile(REGEX);
Matcher m = PT.matcher(value);

m.find(); // find matched subsequence one by one; yes OR no
m.match(); // match entire string; yes OR no

// group

vs code server

安装

1
curl -fsSL https://code-server.dev/install.sh | sh

更多参考这里

配置

详细文档参考这里。默认配置文件路径是 ~/.config/code-server/config.yaml,也可以通过 --bind-addr 参数指定。

配置文件示例。

1
2
3
4
bind-addr: 0.0.0.0:9025
auth: password
password: 67da2351225608a4384150e8
cert: false # 如果使用 https, cert 改为 true

配置 https 自认证

1
2
3
4
5
# 命令行启动
code-server --cert # 默认证书文件在 ~/.local/share/code-server

# 基于配置启动,修改配置文件
cert: true # 设置为 true 会自动创建 self-signed 证书

运行

1
code-server --config /path/to/config --bind-addr ip:port

服务管理

1
2
3
4
5
6
# 配置自启动
sudo systemctl enable code-server@{user}
# 手动启动
sudo systemctl start code-server@{user}
# 手动停止
sudo systemctl stop code-server@{user}

zip - usage

打包

1
2
3
$ zip target.zip target 			# 打包一个文件
$ zip -r target.zip target/ # 打包一个文件夹,-r: 递归
$ zip -re target.zip target/ # 打包一个文件夹并加密,-e: 加密,输入密码提示

on my zsh - install

Install

1
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Config

1
2
$ chsh -s /bin/zsh
$ chsh -s /bin/bash

Plugins

1
$ pip install powerline-status --user

Install

1
2
3
$ cd ~/.oh-my-zsh/custom/plugins/
$ git clone <plugin-repo>
$ vim ~/.zshrc

高亮

1
2
3
$ cd ~/.oh-my-zsh/custom/plugins/
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
$ vim ~/.zshrc

补全

1
2
3
4
5
6
7
8
9
$ cd ~/.oh-my-zsh/custom/plugins/
$ git clone https://github.com/zsh-users/zsh-autosuggestions
$ vim ~/.zshrc

plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
)

算法基础

目录

  • 数据结构
    • 数组
    • 链表
    • 二叉树
  • 算法
    • 快排
    • 二分查找
    • 递归

数据结构

数组

  • 排序
  • 查找
    • 二分查找
    • 包含重复元素的首元素索引
  • 技巧
    • 最大连续和
    • 查找次数超过 n/2 的元素

链表

  • 节点定义
  • 单链表 & 双链表
  • 倒序
  • 合并
    • 两个有序链表合并
    • 多个有序链表合并

二叉树

  • 节点定义

  • 构造

    • 由数组构造二叉树
    • 由遍历结果构造二叉树
  • 遍历

    • 深度优先遍历
    • 广度优先遍历
    • 先、中、后序遍历
  • 应用

    • 两个节点的首个公共父节点
    • 最大连通度
    • 最大连通和

常见解题思路 递归,根据当前节点信息、递归左子树返回数据、递归右子树返回数据,判断是否解决;有时需要额外的变量存储结果。

排序

快排

说明

图解 思路 参考 leetcode,忽略 random 优化的思路步骤如下。

  • 每次排序选第一个元素作为基准元素,将基准元素保存在变量 pivot 中,此时空出一个位置(即基准元素,不必重置元素值)
  • 定义两个游标 left、right 分别从数组两端遍历,其中分别初始化为待排序子数组的左边界索引和右边界索引
  • 然后从右边找一个小于基准值的元素,放到左侧空出的位置,此时该位置空置
  • 然后从左侧找一个大于基准值的元素,放到右侧空出的位置,此时该位置空置
  • 重复前两步,直到左右游标相遇(left == right),此时游标所指位置空置
  • 将基准元素放入游标位置,此时游标左侧的值都小于基准值,游标右侧的值都大于基准值
  • 递归的对游标左侧数组和右侧数组排序
  • 得到一个排序完成的数组

Javascript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 快速排序
* @param nums 数组
* @param left 排序的左边界索引
* @param right 排序的右边界索引
*/
function qsort(nums, left, right) {
if (left >= right) return;

var pivot = nums[left], i = left, j = right;
while (i < j) {
while (i < j && nums[j] > pivot) --j;
nums[i] = nums[j];

while (i < j && nums[i] <= pivot) ++i;
nums[j] = nums[i];
}
nums[i] = pivot;
qsort(nums, left, i-1);
qsort(nums, i+1, right);
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13

var v;
v = [3, 2, 1, 3, 4, 0, -1, 8, 9, 1, 0];
qsort(v, 0, v.length - 1);
console.log(v);

v = [];
qsort(v, 0, v.length - 1);
console.log(v);

v = [5, 4, 3, 2, 1];
qsort(v, 0, v.length - 1);
console.log(v);

输出

1
2
3
[ -1, 0, 0, 1, 1, 2, 3, 3, 4, 8, 9 ]
[]
[ 1, 2, 3, 4, 5 ]

spring aspect

new Bean 问题

在一些场景下,我们需要通过 new 来创建 spring bean,而不是借助 spring 框架,这时会遇到 @Autowired 注解的字段没有初始化的问题(null)。 此时,可以借助 aspect 来解决,具体步骤可以分为三步。

  • 引入依赖
  • 配置 context:spring-configed
  • 命令行添加参数 -javaagent:/path/to/aspectjweaver-{version}.jar

对于 spring boot,启用 context:spring-configed 可以在 Application 类上使用注解 @EnableSpringConfigured 来实现,可能还需要 @EnableAspectJAutoProxy 注解。对于xml配置,可以添加如下代码。

1
2
<aop:aspectj-autoproxy/>
<context:spring-configured/

参考