Spring Annotation Usage Collection

Source: Internet
Author: User
Tags throwable

============== Registration ==================
Label to the class header, register this class with spring to manage the creation
@Repository Data Persistence layer
@Service Business Layer Bean
@Controller Presentation Layer Bean
@Component not recommended for use

====== other Properties 1
<bean name= "" class= ""
Lazy-init= "true"//whether deferred initialization
Scope= the life cycle of the "prototype"//bean
depends-on= "Other beans"//dependent on other beans
/>

There are also corresponding annotations in spring to correspond to
@Lazy
@Scope
@DependsOn

====== Other Properties 2

<bean name= "" class= ""
init-method= "Init"//Initialize Method
Destroy-method= "Close"//destructor method
/>

In spring there are corresponding beans to correspond, of course, the two annotations are built into the JDK
@PostConstruct
@PreDestroy


============== Injection ==================
@Autowired
@ @Resource

Find from the spring context based on the bean type

1 It can be placed on properties, methods, and construction method headers

2 if the implementation class of an interface is unique within the spring container, it can be injected correctly using only @autowired, such as:
@Autowired
Private Sampledao DAO;

3 If an implementation class for an interface is not unique in the spring container
Use @qualifier to specify the name of the injected Bean, as
@Autowired
@Qualifier ("Sampledaoimpl")
Private Sampledao DAO;


4
@Primary specify which is the highest priority among multiple beans, note that it is not used in conjunction with @autowired, but is used in conjunction with @service, such as
@Service @Primary Sampledaoimpl

=================== Notice ====================

The AOP annotations used by spring are divided into three levels:
The @Aspect is placed on the class head and takes this class as a tangent, but this class must be explicitly registered in the Spring container.
The @Pointcut is placed on the method header, defining a pointcut expression that can be referenced by another method.
5 kinds of notifications. Www.2cto.com
@Before, the forward notification, placed on the method header.
@After, the back "finally" notification, placed on the method header.
@AfterReturning, the back "try" notification, placed on the method header, using returning to refer to the method return value.
@AfterThrowing, the post catch notification, placed on the method header, uses throwing to refer to the thrown exception.
@Around, surround notification, put on the method head, this method to determine whether the actual method execution, and must have a return value


=================== Things Propagate Properties ====================
@Transactional

Property Type description
Propagate enumerated optional propagation settings
Propagation
-------------------------------------------------------------
Isolation enum Type Optional isolation level (default value:
Isolation Isolation_default)
-------------------------------------------------------------
Read-only line Boolean read-write things vs read-only things
--------------------------------------------------------------
timeout int seconds for things supermarket settings
---------------------------------------------------------------
Roll back Exception Class (Rollbackfor) a set of class class exception classes that must be rolled back when they are encountered.
instance, must be throwable by default checked exceptions does not perform
Rollback, only unchecked exceptions (subclass of RuntimeException)
Before the transaction is rolled back.
-----------------------------------------------------------------
Regression Exception class name a set of class names, a set of exception class names, which must be rolled back when encountered
Rollbackforclassname must be a subclass of Throwable
Sub-class
------------------------------------------------------------------
Do not rollback exception Class A set of class names,
Norollbackfor must be a subclass of Throwable class name and must not be rolled back when encountered.

------------------------------------------------------------------
Do not roll back the exception class name a set of class names,
Norollbackforclassname must be a subclass of Throwable a set of exception classes that must not be rolled back when encountered
------------------------------------------------------------------
Things
http://blog.csdn.net/zhaofsh/article/details/6285869

How to annotate things: @Transactional

When marked in front of a class, all methods in the marked class are handled by the object

Example:

@Transactional
public class Testservicebean implements Testservice {}

When certain methods in a class do not require things:

@Transactional
public class Testservicebean implements Testservice {
    
     private Testdao DAO;
    
    public void Setdao (Testdao dao) {
         This.dao = DAO;
   }
    
    @Transactional (propagation = propagation.not_supported)
    public list<object> GetAll () {
        return null;
   
    
}


Introduction to things:  
@Transactional (propagation=propagation.required)  
If you have a transaction, add a transaction, and then create a new one (by default)
@Transactional (propagation=propagation.not_supported)  
Container does not open transaction
@Transactional for this method (propagation= propagation.requires_new)  
creates a new transaction regardless of whether a transaction exists, the original hangs, the new execution completes, and continues the old transaction
@Transactional (propagation= propagation.mandatory)  
must be executed in an existing transaction or throw an exception
@Transactional (propagation=propagation.never)   The
must be executed in a transaction that does not have an exception thrown (as opposed to propagation.mandatory)
@Transactional (propagation=propagation.supports)  
If another bean calls this method and declares a transaction in another bean, it uses the transaction. If the other bean does not declare the transaction, then there is no transaction.

Things timeout settings:
@Transactional (timeout=30)//default is 30 seconds

Transaction ISOLATION Level:
@Transactional (isolation = isolation.read_uncommitted)
READ UNCOMMITTED data (dirty read, non-repeatable read) basic not used
@Transactional (isolation = isolation.read_committed)
Read committed data (non-repeatable read and Phantom reads occur)
@Transactional (isolation = isolation.repeatable_read)
REPEATABLE READ (phantom read occurs)
@Transactional (isolation = isolation.serializable)
Serialization

MYSQL: Default to Repeatable_read level
SQL Server: Default is read_committed

Dirty reads: One transaction reads uncommitted update data to another transaction
Non-repeatable READ: In the same transaction, multiple reads of the same data are returned with different results, in other words,
Subsequent reads can be read to the updated data submitted by another transaction. Conversely, "repeatable reads" are repeated in the same transaction
When reading data, it is guaranteed to read the same data, that is, subsequent reads cannot be read to another transaction committed update data
Phantom reads: One transaction reads the insert data that has been committed by another transaction

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring Annotation Usage Collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.