spring中@Data注解详细解析

前言

这个注解一般主要的功能是在实体类上面
主要功能是为了
提供类的get、set、equals、hashCode、canEqual、toString方法

1. 源码

通过学习注解上的源码以及官方文档
可以了解更加透彻

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
    String staticConstructor() default "";
}

@Data : 注解的目标是 类上
提供类的get、set、equals、hashCode、canEqual、toString方法
如果方法值默认不写是系统默认值

引用这个注解的时候还要加入依赖包才可导入

依赖包如下

1.1 lombok

工具类库,可以用简单的注解形式来简化代码,提高开发效率

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>

具体可以通过maven库选择版本号

官网

只有结合这个依赖包工具库和注解@Data才可以进入自动注入

2. 代码

记得添加上依赖包
结合@Data注解进行自动注入

主要功能是:丰富java自动资源管理,自动生成getter、setter、equals、hashCode和toString等等

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}

讲解一下实体类中另外两个注解
@AllArgsConstructor : 注解在类上,有参构造
@NoArgsConstructor : 注解在类上,无参构造

之后生成的代码大致如下

package com.kk.pojo;

public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;

    public int getBookID() {
        return this.bookID;
    }

    public String getBookName() {
        return this.bookName;
    }

    public int getBookCounts() {
        return this.bookCounts;
    }

    public String getDetail() {
        return this.detail;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Books)) {
            return false;
        } else {
            Books other = (Books)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getBookID() != other.getBookID()) {
                return false;
            } else {
                label41: {
                    Object this$bookName = this.getBookName();
                    Object other$bookName = other.getBookName();
                    if (this$bookName == null) {
                        if (other$bookName == null) {
                            break label41;
                        }
                    } else if (this$bookName.equals(other$bookName)) {
                        break label41;
                    }

                    return false;
                }

                if (this.getBookCounts() != other.getBookCounts()) {
                    return false;
                } else {
                    Object this$detail = this.getDetail();
                    Object other$detail = other.getDetail();
                    if (this$detail == null) {
                        if (other$detail != null) {
                            return false;
                        }
                    } else if (!this$detail.equals(other$detail)) {
                        return false;
                    }

                    return true;
                }
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Books;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        int result = result * 59 + this.getBookID();
        Object $bookName = this.getBookName();
        result = result * 59 + ($bookName == null ? 43 : $bookName.hashCode());
        result = result * 59 + this.getBookCounts();
        Object $detail = this.getDetail();
        result = result * 59 + ($detail == null ? 43 : $detail.hashCode());
        return result;
    }

    public String toString() {
        return "Books(bookID=" + this.getBookID() + ", bookName=" + this.getBookName() + ", bookCounts=" + this.getBookCounts() + ", detail=" + this.getDetail() + ")";
    }

    public Books(int bookID, String bookName, int bookCounts, String detail) {
        this.bookID = bookID;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.detail = detail;
    }

    public Books() {
    }
}

3. 优缺点

优点:

  • 自动生成构造器、getter/setter、equals、hashcode、toString等方法,提高了一定的开发效率
  • 让代码变得简洁

缺点:

  • 不支持多种参数构造器的重载
  • 过多的插件容易降低阅读源代码的舒适度
回答: 要使用@Transactional注解和@EnableTransactionManagement注解,首先需要在Spring配置文件中配置事务管理器。然后,在需要进行事务管理的类或方法上添加@Transactional注解。@Transactional注解可以应用在类级别或方法级别,用于标识需要进行事务管理的代码。当代码执行到被@Transactional注解标识的方法时,Spring会自动开启一个事务,并在方法执行完毕后根据事务的执行结果进行提交或回滚。@EnableTransactionManagement注解则用于启用Spring的事务管理功能,通常在配置类上添加该注解。它会扫描带有@Transactional注解的类或方法,并为其创建代理对象,实现事务的管理。\[1\] 具体实现中,AnnotationTransactionAttributeSource类用于解析@Transactional注解,获取事务的属性信息。TransactionAspectSupport类中的commitTransactionAfterReturning方法则是事务提交的相关处理,它会根据事务的状态进行提交操作。\[2\] 另外,还有一些与事务相关的类,如TransactionalEventListenerFactoryBean、FactoryTransactionAttributeSourceAdvisor、TransactionAttributeSource和TransactionInterceptor等,它们在事务管理的过程中起到了重要的作用。\[3\] #### 引用[.reference_title] - *1* *3* [详解 Spring 注解@Transactional事务Aop实现原理](https://blog.csdn.net/heikeb/article/details/126594817)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【Spring源码】Spring事务原理](https://blog.csdn.net/p793049488/article/details/129967258)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农研究僧

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值