Posts

Showing posts with the label Spring

Spring Security for a REST API

https://www.baeldung.com/securing-a-restful-web-service-with-spring-security https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 https://www.youtube.com/watch?v=0pD7YeTAUkk https://www.youtube.com/watch?v=4x-HgnJ31cg

Spring transaction propagation

REQUIRED behaviour Spring  REQUIRED  behaviour means that the same transaction will be used if there is an already opened transaction in the current bean method execution context. If there is no existing transaction the Spring container will create a new one. If multiple methods configured as  REQUIRED  behaviour are called in a nested way they will be assigned  distinct logical transactions  but they will all share the  same physical transaction . In short this means that if an inner method causes a transaction to rollback, the outer method will fail to commit and will also rollback the transaction. Let's see an example: Outer bean @Autowired private TestDAO testDAO ; @Autowired private InnerBean innerBean ; @Override @Transactional ( propagation = Propagation . REQUIRED ) public void testRequired ( User user ) { testDAO . insertUser ( user ); try { innerBean . testRequired (); } catch ( RuntimeException e ){ ...

Design Patterns Used in Spring Framework

Dependency injection or inversion of control (IOC): Dependency injection is a technique in software engineering where an object can supply the dependencies of another object. Such dependency which can be used by an object is known as Service and the injection is the passing of the dependency to an object that uses it. Inversion of control or IOC is a design principle in software engineering through which custom-written computer program portions can receive the control flows from a generic framework. The Spring framework has an IOC container which is responsible for the objects creation, wiring the objects together, configuring these objects and handling the entire life cycle of these objects from their creation until they are completely destroyed. The container has the Dependency Injection (DI) which is used to manage the components present in an application. Such objects are known as Spring Beans. The dependency injection or IOC container is the main principle which is used in t...

Dependency Injection – Field vs Constructor vs Method

In general you have the following three options for injection directly into fields/attributes via explicit setter methods via explicit constructor parameters Field injection This type of injection instruments some kind of reflection mechanism for injecting the required dependencies into the class. While this injection type has the benefit, that it removes clutter code like setter methods or constructor parameters, it has the drawback that these dependencies are invisible. If you look at the class from the outside, you will only see the public methods and may be the constructor. Even if this gives you a very clear idea what services a class provides, it has, in my opinion, this major drawback: When writing tests for this particular class you have to inspect the class to see what are the required dependencies and must use either invoke the DI framework, even for simple tests, or use a kind of reflection mechanism to inject...