Spring Boot
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,
@PostConstructwill be called.If
InitializingBeanis implemented, thenafterPropertiesSet()will be called.InitializingBeanis for Spring Boot to assign attributes with@Autowired.If bean definition contains
init-methodor@Bean(initmethod="..")then it calls the init method.

Running logic on startup
implements
ApplicationListenerrun logic after the Spring context has been initializedimplements
CommandLineRunnerreceives arguments with Stringimplements
ApplicationRunnerreceives arguments withApplicationArguments
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
@Beforeto:Initializing mocking with
MockitoAnnotations.initMocks(this);Or adding
@RunWith(MockitoJUnitRunner.class)on class level
Assigning test target with constructor approach
Or adding
@InjectMocksto 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 runnerLoad related components with:
@SpringBootTestfor looking for a main configuration class (one with@SpringBootApplicationfor instance), and using it to start a Spring application context.@DataJpaTestfor testing JPA repository layer@WebMvcTest(XXController.class)for testing controller layer
Declare test target with
@AutowiredDeclare dependent members with
@MockBeanto add mocking to Spring application context (optional)PS. Spring Boot would initialize embedded DB with below files:
schema.sql
data.sql
Interesting topics
Why
@AutowiredHttpServletRequestis thread safe@Autowired private HttpServletRequest request;request=WebApplicationContextUtils.RequestObjectFactoryWebApplicationContextUtils.RequestObjectFactory.getObject()=WebApplicationContextUtils.currentRequestAttributes().getRequest()WebApplicationContextUtils.currentRequestAttributes()=RequestContextHolder.currentRequestAttributes()RequestContextHolder.currentRequestAttributes()is stored inThreadLocal requestAttributesHolder;
@Beanlite: with
@Componentor 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
Last updated
Was this helpful?