Spring Annotations





@Component for any Spring-managed component, scanned by

to respect the convention:

@Repository for persistence layer – deal with DataAccessException

@Service for service layer

@Controller for presentation layer (spring-mvc) – the dispatcher scans the classes annotated with @Controller and detects @RequestMapping annotations within them. We can only use @RequestMapping on @Controller


@RestController – The Controller only sends back the model (or resource representation) – @Controller + @ResponseBody

@RequestBody – convert incoming JSON

@ResponseBody – annotation means is that the returned value of the method will constitute the body of the HTTP response of accounts is transformed to a format suitable for REST applications, typically JSON or XML

@RequestMapping – mapping web requests onto methods in request-handling classes (produces = MediaType.APPLICATION_JSON_UTF8_VALUE)




@Transactional – start transaction (see Transaction propagations in Spring)







Transaction propagations in Spring

Defaults to Propagation.REQUIRED

TransactionDefinition.ISOLATION_DEFAULT – Use the default isolation level of the underlying datastore.
TransactionDefinition.ISOLATION_READ_COMMITTED – Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
TransactionDefinition.ISOLATION_READ_UNCOMMITTED – Indicates that dirty reads, 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.

TransactionDefinition.PROPAGATION_MANDATORY – Support a current transaction; throw an exception if no current transaction exists.
TransactionDefinition.PROPAGATION_NESTED – Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else.
TransactionDefinition.PROPAGATION_NEVER – Do not support a current transaction; throw an exception if a current transaction exists.
TransactionDefinition.PROPAGATION_NOT_SUPPORTED – Do not support a current transaction; rather always execute non-transactionally.
TransactionDefinition.PROPAGATION_REQUIRED – Support a current transaction; create a new one if none exists.
TransactionDefinition.PROPAGATION_REQUIRES_NEW – Create a new transaction, suspending the current transaction if one exists.
TransactionDefinition.PROPAGATION_SUPPORTS – Support a current transaction; execute non-transactionally if none exists.

TransactionDefinition.TIMEOUT_DEFAULT – Use the default timeout of the underlying transaction system, or none if timeouts are not supported.

@Transactional (rollbackFor=Exception.class)
@Transactional (noRollbackFor=IllegalStateException.class)

XA transaction implementation: JTA – Java Transaction API

JTA – Java Transaction API | ACID | Two-phase commit | Three-phase commit




UserTransaction.begin() – start transaction associated to thread



Spring Test DBUnit



Spring Security

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications.