Usage of @Autowired By default autowired beans are required but this be changed: @Autowired(required=false) Optional is also supported: Disambiguation @Qualifier is used for disambiguation, can be used both for components and beans (names). Autowiring resolution rules: Configuration choices If a class has only a default constructor -> no need to annotate with @Autowired if class had…
Stereotypes
Stereotypes are part of Spring annotations config, used with @ComponentScan. Annotations: @Components – is a generic stereotype for any Spring managed components. @Service – annotates classes at the service layer, code that holds business layer. @Repository – annotation used for classes from the persistence layer. Catches specific exceptions and re-throws them as Spring unified unchecked…
Proxies
Type of proxies available in Spring: JDK Proxy CGLib proxy also called dynamic proxiescomes with JDKonly for Interfacesall interfaces are proxies extra lib included in Springcan intercept protected and public methodsused when interfaces are not availablecannot be applied to final classes or methodsused by Spring Boot Key JDK dynamic proxy CGLIB proxy Basic It can…
Bean Scopes
Bean Scopes
Spring MVC Tests
MVC Test framework Provide support for Spring MVC code, having requests processed through DispatcherServlet without container. It uses MockMvcRequestBuilders and MockMvcResultMatchers. Web environment types: DEFINED_PORT, MOCK, NONE, RANDOM_PORT. Default is Mock @LocalServerPort or @Value(“${local.server.port}”) to get test local port. TestRestTemplate Alternative to RestTemplate for integration tests. Slice testing Performs isolated testing within a slice of an application,…