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

No comments:

Post a Comment