Java annotation learning two

The @param annotation is the meaning of the parameter

@Service is used to mark business layer components (the service layer we usually define uses this)

@Controller is used to mark control layer components (such as action in struts)

@Repository is used to annotate data access components, ie DAO components

@Component generally refers to components. When components are not well classified, we can use this annotation to mark them.

@RequestMapping

RequestMapping is an annotation for processing request address mapping, which can be used on classes or methods. Used on a class, indicating that all methods in the class that respond to requests use this address as the parent path.

The RequestMapping annotation has six attributes. Let's divide it into three categories for description.

1、 value, method;

value: Specify the actual address of the request, the specified address can be in URI Template mode (will be explained later);

method: Specify the method type of the request, GET, POST, PUT, DELETE, etc.;

 

2、 consumes,produces;

consumes: Specify the submitted content type (Content-Type) for processing requests, such as application/json, text/html;

produces: specifies the content type to be returned, which is only returned when the (Accept) type in the request header contains the specified type;

 

3、 params,headers;

params: Specifies that the request must contain certain parameter values ​​for this method to process.

headers: The specified request must contain certain specified header values ​​in order for this method to process the request.

 

The url value of value is the following three categories:

A) can be specified as a common concrete value;

B) Can be specified as a type of value containing a variable (URI Template Patterns with Path Variables);

C) can be specified as a type of value with regular expressions (URI Template Patterns with Regular Expressions);

@Autowired 

 

Spring 2.5 introduced the @Autowired annotation, which can annotate class member variables, methods and constructors to complete the work of autowiring. Eliminate set, get methods through the use of @Autowired.

 

@Override is the metadata of Java5, a sign automatically added to tell you that the following method is inherited from the parent class/interface and needs to be rewritten once, so that it is convenient for you to read, and you are not afraid to forget it.
@Override is pseudo-code, which means overriding (of course not writing it), but writing it has the following benefits:
1> It can be used as a comment for easy reading
2> The compiler can verify for you whether the method name under @Override is all in your parent class, if not, an error will be reported
For example, if you don't write @Override and your method name is wrong, then your compiler can pass it (it thinks this method is a method added by yourself in your subclass)
This flag is used to enhance the program's check at compile time. If the method is not a method that overrides the parent class, the compiler will report an error at compile time. I am just a porter of nature.

Introduction of transaction propagation behavior: 
@Transactional(propagation=Propagation.REQUIRED) 
If there is a transaction, then join the transaction, if not, create a new one (by default)
@Transactional(propagation=Propagation.NOT_SUPPORTED) 
The container does not open a transaction for this method
@Transactional( propagation=Propagation.REQUIRES_NEW) 
Regardless of whether there is a transaction, a new transaction is created, the original is suspended, the new one is executed, and the old transaction is continued.
@Transactional(propagation=Propagation.MANDATORY) 
must be in an existing transaction Execute, otherwise throw exception
@Transactional(propagation=Propagation.NEVER) 
must be executed in a transaction that does not have, otherwise throw exception (as opposed to Propagation.MANDATORY)
@Transactional(propagation=Propagation.SUPPORTS) 
if other beans call this method , declare transactions in other beans, then use transactions. If other beans do not declare transactions, then do not use transactions.

Transaction timeout setting:
@Transactional(timeout=30) //The default is 30 seconds

事务隔离级别:
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
读取未提交数据(会出现脏读, 不可重复读) 基本不使用
@Transactional(isolation = Isolation.READ_COMMITTED)
读取已提交数据(会出现不可重复读和幻读)
@Transactional(isolation = Isolation.REPEATABLE_READ)
可重复读(会出现幻读)
@Transactional(isolation = Isolation.SERIALIZABLE)
串行化

MYSQL: 默认为REPEATABLE_READ级别
SQLSERVER: 默认为READ_COMMITTED

脏读 : 一个事务读取到另一事务未提交的更新数据
不可重复读 : 在同一事务中, 多次读取同一数据返回的结果有所不同, 换句话说, 
后续读取可以读到另一事务已提交的更新数据. 相反, "可重复读"在同一事务中多次
读取数据时, 能够保证所读数据一样, 也就是后续读取不能读到另一事务已提交的更新数据
幻读 : 一个事务读到另一个事务已提交的insert数据

@Transactional注解中常用参数说明

参 数 名 称

功 能 描 述

readOnly

该属性用于设置当前事务是否为只读事务,设置为true表示只读,false则表示可读写,默认值为false。例如:@Transactional(readOnly=true)

rollbackFor

该属性用于设置需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,则进行事务回滚。例如:

指定单一异常类:@Transactional(rollbackFor=RuntimeException.class)

指定多个异常类:@Transactional(rollbackFor={RuntimeException.class, Exception.class})

 续表)

参 数 名 称

功 能 描 述

rollbackForClassName

该属性用于设置需要进行回滚的异常类名称数组,当方法中抛出指定异常名称数组中的异常时,则进行事务回滚。例如:

指定单一异常类名称:@Transactional(rollbackForClassName="RuntimeException")

指定多个异常类名称:@Transactional(rollbackForClassName={"RuntimeException","Exception"})

noRollbackFor

该属性用于设置不需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,不进行事务回滚。例如:

指定单一异常类:@Transactional(noRollbackFor=RuntimeException.class)

指定多个异常类:@Transactional(noRollbackFor={RuntimeException.class, Exception.class})

noRollbackForClassName

该属性用于设置不需要进行回滚的异常类名称数组,当方法中抛出指定异常名称数组中的异常时,不进行事务回滚。例如:

指定单一异常类名称:@Transactional(noRollbackForClassName="RuntimeException")

指定多个异常类名称:

@Transactional(noRollbackForClassName={"RuntimeException","Exception"})

propagation

该属性用于设置事务的传播行为,具体取值可参考表6-7。

例如:@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)

isolation

该属性用于设置底层数据库的事务隔离级别,事务隔离级别用于处理多事务并发的情况,通常使用数据库的默认隔离级别即可,基本不需要进行设置

timeout

该属性用于设置事务的超时秒数,默认值为-1表示永不超时

注意的几点:
1 @Transactional 只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能.

2用 spring 事务管理器,由spring来负责数据库的打开,提交,回滚.默认遇到运行期例外(throw new RuntimeException("注释");)会回滚,即遇到不受检查(unchecked)的例外时回滚;而遇到需要捕获的例外(throw new Exception("注释");)不会回滚,即遇到受检查的例外(就是非运行时抛出的异常,编译器会检查到的异常叫受检查例外或说受检查异常)时,需我们指定方式来让事务回滚要想所有异常都回滚,要加上 @Transactional( rollbackFor={Exception.class,其它异常}) .如果让unchecked例外不回滚: @Transactional(notRollbackFor=RunTimeException.class)
如下:
@Transactional(rollbackFor=Exception.class) //指定回滚,遇到异常Exception时回滚
public void methodName() {
throw new Exception("注释");

}
@Transactional(noRollbackFor=Exception.class)//指定不回滚,遇到运行期例外(throw new RuntimeException("注释");)会回滚
public ItimDaoImpl getItemDaoImpl() {
throw new RuntimeException("注释");
}

3、@Transactional 注解应该只被应用到 public 可见度的方法上。 如果你在 protected、private 或者 package-visible 的方法上使用 @Transactional 注解,它也不会报错, 但是这个被注解的方法将不会展示已配置的事务设置。


4、@Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。然而,请注意仅仅 @Transactional 注解的出现不足于开启事务行为,它仅仅 是一种元数据,能够被可以识别 @Transactional 注解和上述的配置适当的具有事务行为的beans所使用。上面的例子中,其实正是 元素的出现 开启 了事务行为。


5、Spring团队的建议是你在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。你当然可以在接口上使用 @Transactional 注解,但是这将只能当你设置了基于接口的代理时它才生效。因为注解是不能继承的,这就意味着如果你正在使用基于类的代理时,那么事务的设置将不能被基于类的代理所识别,而且对象也将不会被事务代理所包装(将被确认为严重的)。因此,请接受Spring团队的建议并且在具体的类上使用 @Transactional 注解。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326407910&siteId=291194637