Spring Life Cycle:
BeanFacory vs ApplicationContext:
@Order
BeanFacory vs ApplicationContext:
Annotation | Purpose | Scope |
---|---|---|
@Autowired | Auto-wires dependencies by type | Constructor, Field, Setter |
@Qualifier | Specifies which bean to inject | With @Autowired |
@Primary | Sets default bean when multiple candidates exist | On bean definitions |
@Bean | Declares a bean in a config class | @Configuration classes |
@Value | Injects property values or expressions | Fields |
@Profile | Activates beans for specific environments | On components/config classes |
@Configuration | Declares a configuration class | Application setup |
@ComponentScan | Scans for Spring components | Main class or config classes |
@Scope | Defines bean lifecycle (singleton , prototype ) | On beans |
@PostConstruct | Initializes beans after dependency injection | Bean lifecycle |
@PreDestroy | Cleans up before bean destruction | Bean lifecycle |
Approach | Pros | Cons |
---|---|---|
ObjectFactory<T> | Simple, lightweight | Some boilerplate code |
Provider<T> | Better readability, cleaner | Requires jakarta.inject |
@Lookup Method Injection | No need for Provider | Less 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
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.
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:
Post a Comment