[Java Notes] Annotation understanding and common Annotation examples

One: Understanding of annotations:

Annotation overview

In JavaSE, the purpose of using annotations is relatively simple, such as marking obsolete functions, ignoring warnings, etc. Annotations play a more important role in JavaEE/Android, for example, to configure any aspect of the application, replacing the cumbersome code and XML configuration left in the old version of JavaEE.

The future development model is based on annotations. JPA is based on annotations. Spring 2.5 and above are based on annotations. Hibernate 3.x and later are also based on annotations. Now part of Struts2 is also based on annotations. Annotations are a trend , to a certain extent, it can be said: framework = annotation + reflection + design pattern.

Two: Common Annotation examples

When using Annotation, add the @ symbol in front of it, and use the Annotation as a modifier . Used to decorate the program elements it supports

Example 1: Generate documentation-related annotations

@author indicates the author who developed this type of module, used between multiple authors, split

@version indicates the version of this type of module

@see refers to the turn, that is, the related topic

@since from which version was added

@param The description of a parameter in the method, if there is no parameter, it cannot be written

@return Description of the return value of the method, if the return value type of the method is void, it cannot be written

@exception explains the exceptions that may be thrown by the method. If the method does not explicitly throw exceptions with throws, it cannot be written

in

   The three tags @param @return and @exception are only used for methods.

   Format requirements of @param: @param parameter name parameter type parameter description

   Format requirements for @return: @return return value type return value description

   Format requirements of @exception: @exception exception type exception description

   @param and @exception can be multiple in parallel

for example:

package com.annotation.javadoc;
/**
* @author shkstart
* @version 1.0
* @see Math.java
*/
public class JavadocTest {
/**
* 程序的主方法,程序的入口
* @param args String[] 命令行参数
*/
public static void main(String[] args) {
}

/**
* 求圆面积的方法
* @param radius double 半径值
* @return double 圆的面积
*/
public static double getArea(double radius){
return Math.PI * radius * radius;
}
}

Example 2: Format check at compile time (three basic annotations built into JDK)

@Override : Restricted overriding the parent class method, this annotation can only be used for methods

@Deprecated: Used to indicate that the modified element (class, method, etc.) is obsolete. Usually because the modified structure is dangerous or better alternatives exist

@SuppressWarnings : suppress compiler warnings

Example:

package com.annotation.javadoc;
public class AnnotationTest{
    public static void main(String[] args) {
        @SuppressWarnings("unused")
        int a = 10;
    }
    
    @Deprecated
    public void print(){
        System.out.println("过时的方法");

    }
    
    @Override
    public String toString() {

        return "重写的toString方法()";

    }
}

Example Three: Tracking Code Dependencies and Implementing Alternative Configuration File Functions

Servlet3.0 provides annotations, which make it no longer necessary to deploy Servlets in the web.xml file.

Example:

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException { }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        doGet(request, response);
    } }
<servlet>

<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>

<url-pattern>/login</url-pattern>

</servlet-mapping>

Management of "transactions" in the spring framework

 @Transactional(propagation=Propagation.REQUIRES_NEW,
            isolation=Isolation.READ_COMMITTED,readOnly=false,timeout=3)

    public void buyBook(String username, String isbn) {
        //1.查询书的单价
        int price = bookShopDao.findBookPriceByIsbn(isbn);

        //2. 更新库存
        bookShopDao.updateBookStock(isbn);

        //3. 更新用户的余额
        bookShopDao.updateUserAccount(username, price);
    }
<!-- 配置事务属性 -->

<tx:advice transaction-manager="dataSourceTransactionManager" id="txAdvice">
<tx:attributes>
<!-- 配置每个方法使用的事务属性 -->
<tx:method name="buyBook" propagation="REQUIRES_NEW"
isolation="READ_COMMITTED" read-only="false" timeout="3" />
</tx:attributes>
</tx:advice>

thanks for watching! ! !

Guess you like

Origin blog.csdn.net/qq_64976935/article/details/129561951