Development Notes
  • Introduction
  • Programming Langauges
    • Java
      • Cache
      • Java Fundamentals
      • Multithreading & Concurrency
      • Spring Boot
        • Spring Security
        • Development tips
      • ORM
        • Mybatis
      • Implementation & Testing
    • Node.js
      • Asynchronous Execution
      • Node.js Notes
    • Python
      • Memo
  • Data Structure & Algorithm
  • Database
  • Design Pattern
  • AWS Notes
    • Services
      • API Gateway
      • CloudHSM
      • Compute & Load Balancing
        • Auto Scaling Group
        • EC2
        • ECS
        • ELB
        • Lambda
      • Data Engineering
        • Athena
        • Batch
        • EMR
        • IoT
        • Kinesis
        • Video Streaming
        • Quicksight
      • Deployment
        • CloudFormation
        • Code Deploy
        • Elastic Beanstalk
        • OpsWorks
        • SAM
        • SSM
      • ElasticSearch
      • Identity & Federation
        • Directory Service
        • IAM
        • Organizations
        • Resource Access Manager (RAM)
        • SSO
        • STS
      • KMS
      • Management Tools
        • Catalog
        • CloudTrail
        • CloudWatch
        • Config
        • Cost Allocation Tags
        • GuardDuty
        • Savings Plans
        • Trusted Advisor
        • X-Ray
      • Migration
        • Cloud Migration: The 6R
        • Disaster Recovery
        • DMS
        • VM Migrations
      • Networking
        • ACM
        • CloudFront
        • Direct Connect
        • EIP & ENI
        • Network Security
        • PrivateLink
        • Route53
        • VPC
        • VPN
      • Service Commnucation
        • Amazon MQ
        • SNS
        • SQS
        • Step Functions
        • SWF
      • Storage
        • Aurora
        • DynamoDB
        • EBS
        • EFS
        • ElastiCache
        • RDS
        • Redshift
        • S3
        • Storage Gateway
      • Other Services
        • Alexa for Business, Lex, Connect
        • AppStream 2.0
        • CloudSearch
        • Comprehend
        • Data Tools
        • Elastic Transcoder
        • Mechanical Turk
        • Rekognition
        • WorkDocs
        • WorkSpaces
    • Well Architect Framework
      • Security
      • Reliability
      • Performance Effeciency
      • Cost Optimization
      • Operational Excellence
    • Labs
      • Webserver Implementation
      • ELB Implementation
      • Auto-scaling Implementation
      • A 3-tier Architecture In VPC
  • Architecture
    • Security
  • Spark
    • Memo
  • Conference Notes
    • Notes of JCConf 2017
  • AI Notes
Powered by GitBook
On this page

Was this helpful?

  1. Programming Langauges
  2. Java

Spring Boot

PreviousMultithreading & ConcurrencyNextSpring Security

Last updated 2 years ago

Was this helpful?

Key Concepts

    • Configure beans according to the jars under classpath

  • Spring Bean Life Cycle

  • This 3 step happens in the Bean Creation Life-Cycle Callback:

    • When an object constructor is done, @PostConstruct will be called.

    • If InitializingBean is implemented, then afterPropertiesSet() will be called. InitializingBean is for Spring Boot to assign attributes with @Autowired.

    • If bean definition contains init-method or @Bean(initmethod="..") then it calls the init method.

  • Running logic on startup

    • implements ApplicationListener run logic after the Spring context has been initialized

    • implements CommandLineRunner receives arguments with String

    • implements ApplicationRunner receives arguments with ApplicationArguments

Spring Cloud

  • Quickly enable and configure the common patterns between Spring Boot applications and build large distributed systems.

    • Eureka - Service Discovery

      • Analogy: ECS / API Gateway + Lambda

    • Feign - Declarative HTTP Client

      • Analogy: Route53 / ELB

    • Ribbon - Load balancing

      • Analogy: ELB

    • Hystrix - Circuit Breaker

      • Analogy: AWS App Mesh

    • Zuul - Intelligent Routing

      • Analogy: AWS API Gateway

Automatic Test Implementation

  • Unit Tests

    • Declare test target and dependent members (with Mock / Spy)

    • Declare setUp with @Before to:

      • Initializing mocking with MockitoAnnotations.initMocks(this);

        • Or adding @RunWith(MockitoJUnitRunner.class) on class level

      • Assigning test target with constructor approach

        • Or adding @InjectMocks to test target

    • Within tests

      • Setting up given, when then for the mocks

      • Asserting value of results

      • Verifying execution times

      • Asserting arguments in methods invoked with ArgumentCaptor

  • Integration Tests

    • Annotations on class level

      • RunWith(SpringRunner.class) for JUnit to invoke the runner

      • Load related components with:

        • @SpringBootTest for looking for a main configuration class (one with @SpringBootApplication for instance), and using it to start a Spring application context.

        • @DataJpaTest for testing JPA repository layer

        • @WebMvcTest(XXController.class) for testing controller layer

    • Declare test target with @Autowired

    • Declare dependent members with @MockBean to add mocking to Spring application context (optional)

    • PS. Spring Boot would initialize embedded DB with below files:

      • schema.sql

      • data.sql

Interesting topics

  • Why @Autowired HttpServletRequest is thread safe

    • @Autowired private HttpServletRequest request;

    • request = WebApplicationContextUtils.RequestObjectFactory

    • WebApplicationContextUtils.RequestObjectFactory.getObject() = WebApplicationContextUtils.currentRequestAttributes().getRequest()

    • WebApplicationContextUtils.currentRequestAttributes() = RequestContextHolder.currentRequestAttributes()

    • RequestContextHolder.currentRequestAttributes() is stored in ThreadLocal requestAttributesHolder;

  • @Bean

    • lite: with @Component or event without it. Beans would be created by every invocation.

    • standard: with @Configuration. the bean (proxy created with CGLIB) is created when invoked at first time and subsequent invocations would return the same bean.

Reference

Create your own Spring Boot Starter
@EnableAutoConfiguration