JPA Methods
Author: Armando Marques
Spring Transactions
Spring Transaction management
Spring Boot Core
MVC Rest
SpringBoot MVC Rest
Spring Boot JPA
Spring Boot JPA
Tests
Spring Test
Spring Actuator
Spring Actuator
Spring AOP
Spring AOP
JPA Pagination and Sorting
Spring data
Spring Data encapsulate multiple modules: Spring Data JPA Interfaces JDBC – Old java api.Spring JDBC – Spring abstraction to JDBC (JDBC Template)Spring Data JPA -Spring JPA supported implementation.Spring Data JDBC – New asynchronous implementationsR2BC – Reactive streams solution. Persistence Is the first-level cache where all entities are fetched from database or saved to, there are…
Spring MVC Entities
Annotations Name Description @Model Interface, holder for model attributes @ModelMap Class, in ModelMap attibute key can be omitted and the value of the attribute will be use to generate the key. @ModelAndView ModelAndView it’s a container for both View object and ModelMap @ModelAttribute Annotation that binds a method parameter or method return method to a…
Spring Security Entities
Spring Security Entities
Spring Security
Security concepts Architecture SecurityContextHolder – The SecurityContextHolder is where Spring Security stores the details of who is authenticated. SecurityContext – is obtained from the SecurityContextHolder and contains the Authentication of the currently authenticated user. Authentication – Can be the input to AuthenticationManager to provide the credentials a user has provided to authenticate or the current…
Spring Security II
Security filters Spring Security MVC is based on Server Filters. DelegatingFilterProxy – Filter implementation that allows bridging between servlet container lifecycle and ApplicationContext. This filter follows standard servlets containers mechanisms but delegates all work to a spring bean that implements filter. FilterChainProxy – special filter, provided by Spring Security that allows delegating to many filter…
Method security
Spring Security’s method authorization support is handy for: And since Method Security is built using Spring AoP. Annotation @EnableMethodSecurity Annotation @Secured is a legacy option to authorize invocations, superseded by @PreAuthorized. JSR 250 annotations correspond to @RolesAllowed, @PermitAll and @DenyAll. Annotations @PreAuthorize and @PostAuthorize – verifies condition before or after method invocation. @PreFilter and @PostFilter…
SpEL and Value
Spring EL Accessing properties: $ – value# – expression/executionValue- @Value(“${bean.message}”)System environment variable- @Value(“#{systemEnvironment[‘SOME_ENV_VARIABLE’]}”) It casts to the required type: @Value(“${daily.limit}”) int maxTransferPerDay@Value(“#{environment[‘daily.limit’]}”)int maxTransferPerDay; But values are handled as String: @Value(“#{new Integer(environment[‘daily.limit’]) * 2 }”) -> OK@Value (“#{new java.net.URI(environment[‘home.page’]).host}”) -> OK@Value (“#{daily.limit * 2}”}) -> not OK Default values: @Value(“${daily.limit:1000}”)@Value(“#{environment[‘daily.limit’]?:1000}”) https://www.baeldung.com/spring-expression-language @Value This annotation can be…
SpringBoot Lifecycle extensions
BeanFactoryPostProcessor BeanFactoryPostProcessor operates on bean configuration metadata, it reads the configuration metadata and can change it before the container instantiates the beans. This interfaces can be extended to provide further customization. BeanPostProcessor The BeanPostProcessor interface can be itself customized logic to default steps, when Spring container finishes instantiating, configuring, and initializing a bean, several implementations…
Autowiring and others
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,…
Spring Exception Handler
Error Handling for REST with Spring Before Spring 3.2 the two main approaches to handle exceptions where HandleExceptionResolver or @ExceptionHandler annotation. The it was added @ControllerAdvice and ResponseStatusException. Controller level (MVC) This strategy only works for active controllers is not global to the entire application. HandleExceptionResolver This is enabled by default in DispatchServlet. DefaultHandlerExceptionResolver Used…
Reactive programming
Reactive programming