Tuesday, April 17, 2018

Spring Framework

Spring Life Cycle:


BeanFacory vs ApplicationContext:


AnnotationPurposeScope
@AutowiredAuto-wires dependencies by typeConstructor, Field, Setter
@QualifierSpecifies which bean to injectWith @Autowired
@PrimarySets default bean when multiple candidates existOn bean definitions
@BeanDeclares a bean in a config class@Configuration classes
@ValueInjects property values or expressionsFields
@ProfileActivates beans for specific environmentsOn components/config classes
@ConfigurationDeclares a configuration classApplication setup
@ComponentScanScans for Spring componentsMain class or config classes
@ScopeDefines bean lifecycle (singleton, prototype)On beans
@PostConstructInitializes beans after dependency injectionBean lifecycle
@PreDestroyCleans up before bean destructionBean lifecycle
@Order




ApproachProsCons
ObjectFactory<T>Simple, lightweightSome boilerplate code
Provider<T>Better readability, cleanerRequires jakarta.inject
@Lookup Method InjectionNo need for ProviderLess flexible, needs subclassing


TransactionManagement:
Spring Transaction Abstraction:

The key to the Spring transaction abstraction is defined by the org.springframework.transaction.PlatformTransactionManager interface, which is as follows −
public interface PlatformTransactionManager {
   TransactionStatus getTransaction(TransactionDefinition definition);
   throws TransactionException;
   
   void commit(TransactionStatus status) throws TransactionException;
   void rollback(TransactionStatus status) throws TransactionException;
}
The TransactionDefinition is the core interface of the transaction support in Spring and it is defined as follows −
public interface TransactionDefinition {
   int getPropagationBehavior();
   int getIsolationLevel();
   String getName();
   int getTimeout();
   boolean isReadOnly();
}

Isolation:

TransactionDefinition.ISOLATION_DEFAULT
This is the default isolation level.

TransactionDefinition.ISOLATION_READ_UNCOMMITTED
Indicates that dirty reads, non-repeatable reads, and phantom reads can occur.
TransactionDefinition.ISOLATION_READ_COMMITTED
Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
TransactionDefinition.ISOLATION_REPEATABLE_READ
Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
TransactionDefinition.ISOLATION_SERIALIZABLE
Indicates that dirty reads, non-repeatable reads, and phantom reads are prevented.

Propagation:

TransactionDefinition.PROPAGATION_REQUIRED

Supports a current transaction; creates a new one if none exists.
TransactionDefinition.PROPAGATION_REQUIRES_NEW
Creates a new transaction, suspending the current transaction if one exists.
TransactionDefinition.PROPAGATION_MANDATORY
Supports a current transaction; throws an exception if no current transaction exists.
TransactionDefinition.PROPAGATION_NESTED
Executes within a nested transaction if a current transaction exists.
TransactionDefinition.PROPAGATION_NEVER
Does not support a current transaction; throws an exception if a current transaction exists.
TransactionDefinition.PROPAGATION_SUPPORTS
Supports a current transaction; executes non-transactionally if
TransactionDefinition.PROPAGATION_NOT_SUPPORTED
Does not support a current transaction; rather always execute nontransactionally.
TransactionDefinition.TIMEOUT_DEFAULT
Uses the default timeout of the underlying transaction system, or none if timeouts are not supported.
TransactionStatus:
public interface TransactionStatus extends SavepointManager {
   boolean isNewTransaction();
   boolean hasSavepoint();
   void setRollbackOnly();
   boolean isRollbackOnly();
   boolean isCompleted();
}

No comments:

Security Certificates

  1. Cryptography Basics Understand Key Concepts : Encryption, decryption, hashing, and digital signatures. Key terms: confidentiality, inte...