Friday, 30 September 2016

spring

1. @componentScan
  • ask spring to scan @component/@configuration/@service class in specified base package

2. @configuration
  • tell spring that this class defines one or more @bean
  • spring will load beans into it’s context before we even request it
  • this is to make sure all the beans are properly configured and application fail-fast if something goes wrong
  • the default scope of spring beans is singleton, meaning that the bean will be created only once for the whole application

3. @component
  • spring will: scan our application for classes annotated with @Component. 
  • spring will: instantiate them and inject any specified dependencies into them

4. @autowired
  • auto dependency injection

5. core problem that spring resolve - DI

before
@RestController
public class WelcomeController {
    private WelcomeService service = new WelcomeService();
    @RequestMapping("/welcome")
    public String welcome() {
        return service.retrieveWelcomeMessage();
    }
}

after
@Component
public class WelcomeService {
    //Bla Bla Bla
}
@RestController
public class WelcomeController {
    @Autowired
    private WelcomeService service;
    @RequestMapping("/welcome")
    public String welcome() {
        return service.retrieveWelcomeMessage();
    }
}

6. Instantiate bean
  • explicitly - use applicationContext 
  • implicitly - use autowire 

7. spring use jackson json library to marshal object into json

8. yaml file

reference
1. sprint boot vs. spring mvc vs. spring
2. yaml file example

Tuesday, 6 September 2016

unix

1. vi
  • move within line: 0(begin), $(end), w(next), b(previous)
  • move within file: 1G(begin), G(end), nG(line) 
  • move between screen: ctrl+f(forward), ctrl+b(back), ctrl+d(forward half), ctrl+u(back half)
  • u(undo) 
  • edit: i(insert), x(delete), o(insert line before), O(insert line after), d0(delete from 0), d$(delete to $), dd(delete line), ndd(delete n line), yy(copy line), nyy(copy n line), p(paste),
  • search: /(search forward), ?(search backward), n(next forward), N(next backward)
  •  replace: :%s/text1/text2/g
  • line number: ^g(show current line number and total line number), :set number, :set nonumber, :1,10y(copy line 1-10)

2. ls
  • ls -altr  //a=all, l=long_format, t=sort_by_time, r=reverse_order

3. less
  • F (become tail -f)
  • ctrl+g (show statistics)
  • v (edit)
  • h (help)
  • -N (display line number)
  • G (go to end of file)
  • g (go to start of file)

4. grep
  • grep -n pattern file

5. cron

6. get information of linux machine
  • primary ip address: /sbin/ifconfig -a
  • OS version: uname -a
  • ps -ef | grep proc_name
  • hostname ip conversion: nslookup [address] [hostname]

7. ssh tunnelling
       ~/.ssh/config

       host wstasapp016
       LocalForward 50404 10.200.10.222:1521

       to test: ssh -p 50404 localhost

8. keep console busy
  • prstat
  • while sleep 300; do echo -ne "\a"; done &

9. sudo and su
  • sudo     //ask for root permission
  • su         //switch to another user
  • sudo su - user  //open a login shell as user

10. shell argument
  • "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}
  • "$#" is the number of positional parameters
  • "$0" is the name of the shell or shell script
  • -z string true if the string is null
  • -n is the opposite of -z
  • foo=${variable:-default}  # If variable not set or null, use default.

11. ping -c 3                               //ping with 3 packet


reference
1. setup ssh tunnel using putty
2. ssh tunnelling using config file
3. dollar sign shell variable
4. linux test command

Monday, 5 September 2016

regex

1. examples
  • regexr.com                 //playground
  • /pattern/g                   //global
  • /pattern/                     //1st occurrence
  • [enl]                           //individual match of e n l
  • [^enl]                         //individual match except e n l
  • [a-z]
  • [A-Z]
  • [0-9]
  • .                                  //everything except newline
  • \n                                //new line
  • \w                               //word char (including digits), 1 char is 1 match
  • \W                              //non word char
  • \d                                //digit, 1 digit is 1 match
  • \D                               //non digit
  • \s                                //white space
  • \S                               //non white space
  • [\s\S]                          //everything including newline
  • [^i]                             //i as 1st char in 1st line
  • [^i]/m                         //i as 1st char in multi line
  • \.                                 //period
  • (old)                           //capture group that select 'old'
  • (?:old)                        //non capture group that select 'old'
  • \1 or $1                      //1st capture group
  • g(?=old)                     //look ahead, 'g' that's followed by 'old'
  • g(?!old)                      //negative look ahead, g' that's not followed by 'old'
  • +                                 //one or more of a pattern
  • *                                 //zero or more of a pattern
  • ?                                 //zero or one of a pattern
  • {3}                             //3 copies of a pattern
  • {3,}                            //3 or more copies of a pattern
  • {3,4}                          //3 or 4 copies of a pattern


greedy vs lazy
greedy:
<.+>                                              //matches <em>Hello World</em>
\d+                                                //matches 12345

lazy
<.+?>                                            //? after + tells it to repeat as few as possible and matches <em>
\d+?                                              //matches 1

special character
  • ^ $                                       //begin end of line
  • \< \>                                    //begin end of word in vim
  • \                                          //give special meaning to next character
  • ?                                         //change preceding quantifier to lazy
  • /                                          //regex delimiter, mark the beginning and end of regex
common pattern
  • \b                                        //word boundary
  •  .|\n                                                       //any character or new line
  • <tag[^>]*>((.|\n)*?)<\/tag>                 //html tag
  • :%s/\([a-zA-Z]*\):/"\1":/g                    //search and replace through out whole file
  • :%s/'/"/g
assertion
  • is a condition that needs to be met for preceding or following characters, but is NOT part of the regex match result

in java
  • \\                                         //\ is special character in java, need to escape it

online playground: https://regex101.com/
best tutorial: i-wanna-use-regex-but-what-does-it-all-mean


vi search and replace example
  • :%s/old/new/g                           //all lines
  • :s/old/new/gi                             //current line, ignore case
  • :s/\<old\>/new/                         //old is a whole word
  • %s:/g:abc:g                               //special char, use any char as delimit, in this case :
  • :g/^baz/s/foo/bar/g                   //change in each line starting with 'baz'
when searching:

., *, \, [, ^, and $ are metacharacters.
+, ?, |, &, {, (, and ) must be escaped to use their special function


reference
1. best regex website
2. good article explaining regex in javascript 
3. java regular expression
4. rexegg tutorial
5. greedy and lazy match
6. difference between :g and :%s commands in vim
7. regex online
8. i-wanna-use-regex-but-what-does-it-all-mean