LOMBOK常用注解说明

lombok的安装说明,上一篇已经说过了这里就不再说明。

如果还可以的话可以打上一波
**

常用的注解

**

@Slf4j 由于自己开启日志需要写上

private  final Logger logger = LoggerFactory.getLogger(XXX.class);

为了方便,可以使用注解@Slf4j来直接使用log对象,简化了一行代码。。。
原来使用的就是logger.info("");这样的代码,现在就可以使用log.info("")
在使用中需要引入jar包import lombok.extern.slf4j.Slf4j;
在这里插入图片描述

@Setter
生成setter方法,final变量不包含


//原始类
@Setter
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final int type = 0;
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final int type = 0;
 
    public TestEntity() {
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
}

@Getter
生成getter方法,final变量不包含

//原始类
@Getter
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public String getName() {
        return this.name;
    }
 
    public Integer getAge() {
        return this.age;
    }
 
    public String getType() {
        this.getClass();
        return "person";
    }
}

@NoArgsConstructor : 生成无参构造
@AllArgsConstructor : 生成全部参数构造
@RequiredArgsConstructor : 通常与@NoNull注解连用,表示生成指定的有参构造

//原始类
@RequiredArgsConstructor
public class TestEntity {
 
    private String name;
    @NonNull
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    @NonNull
    private Integer age;
    private final String type = "person";
 
    @ConstructorProperties({"age"})
    public TestEntity(@NonNull Integer age) {
        if(age == null) {
            throw new NullPointerException("age");
        } else {
            this.age = age;
        }
    }
}

@ToString :生成所有属性的toString()方法
@EqualsAndHashCode:生成equals()方法和hashCode方法
@Data(常用): @Data=@Setter+@Getter+@EqualsAndHashCode+@NoArgsConstructor


//原始类
@Data
public class TestEntity {
    @Setter(AccessLevel.PRIVATE)
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
 
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public String getName() {
        return this.name;
    }
 
    public Integer getAge() {
        return this.age;
    }
 
    public String getType() {
        this.getClass();
        return "person";
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
    public boolean equals(Object o) {
     ...
    }
 
    protected boolean canEqual(Object other) {
        return other instanceof TestEntity;
    }
 
    public int hashCode() {
      ...
    }
 
    public String toString() {
        return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
    }
 
    private void setName(String name) {
        this.name = name;
    }
}

@Builder:构造Builder模式的结构。通过内部类Builder()进行构建对象。


//原始类
@Builder
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    @ConstructorProperties({"name", "age"})
    TestEntity(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
 
    public static TestEntity.TestEntityBuilder builder() {
        return new TestEntity.TestEntityBuilder();
    }
 
    public static class TestEntityBuilder {
        private String name;
        private Integer age;
 
        TestEntityBuilder() {
        }
 
        public TestEntity.TestEntityBuilder name(String name) {
            this.name = name;
            return this;
        }
 
        public TestEntity.TestEntityBuilder age(Integer age) {
            this.age = age;
            return this;
        }
 
        public TestEntity build() {
            return new TestEntity(this.name, this.age);
        }
 
        public String toString() {
            return "TestEntity.TestEntityBuilder(name=" + this.name + ", age=" + this.age + ")";
        }
    }
}
 
//Builder模式使用方法
@Test
public  void test(){
    TestEntity testEntity = TestEntity.builder()
                    .name("java")
                    .age(18)
                    .build();
}
}

@Value:
与@Data相对应的@Value, 两个annotation的主要区别就是如果变量不加@NonFinal ,@Value会给所有的弄成final的。当然如果是final的话,就没有set方法了。

//原始类
@Value
public class TestEntity {
    @Setter(AccessLevel.PRIVATE)
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public final class TestEntity {
    private final String name;
    private final Integer age;
    private final String type = "person";
 
    @ConstructorProperties({"name", "age"})
    public TestEntity(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return this.name;
    }
 
    public Integer getAge() {
        return this.age;
    }
 
    public String getType() {
        this.getClass();
        return "person";
    }
 
    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof TestEntity)) {
            return false;
        } else {
            TestEntity other;
            label44: {
                other = (TestEntity)o;
                String this$name = this.getName();
                String other$name = other.getName();
                if(this$name == null) {
                    if(other$name == null) {
                        break label44;
                    }
                } else if(this$name.equals(other$name)) {
                    break label44;
                }
 
                return false;
            }
 
            Integer this$age = this.getAge();
            Integer other$age = other.getAge();
            if(this$age == null) {
                if(other$age != null) {
                    return false;
                }
            } else if(!this$age.equals(other$age)) {
                return false;
            }
 
            String this$type = this.getType();
            String other$type = other.getType();
            if(this$type == null) {
                if(other$type != null) {
                    return false;
                }
            } else if(!this$type.equals(other$type)) {
                return false;
            }
 
            return true;
        }
    }
 
    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        String $name = this.getName();
        int result1 = result * 59 + ($name == null?43:$name.hashCode());
        Integer $age = this.getAge();
        result1 = result1 * 59 + ($age == null?43:$age.hashCode());
        String $type = this.getType();
        result1 = result1 * 59 + ($type == null?43:$type.hashCode());
        return result1;
    }
 
    public String toString() {
        return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
    }
}

@Synchronized: 同步方法


//原始类
public class TestEntity {
    private String name;
 
    private Integer age;
 
    private final String type = "person";
    @Synchronized
    public void write(){
        //do something
    }
}
//反编译的类
public class TestEntity {
    private final Object $lock = new Object[0];
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public void write() {
        Object var1 = this.$lock;
        synchronized(this.$lock) {
            ;
        }
    }
}

@Cleanup @@SneakyThrows:自动调用close方法关闭资源。

//原始类
public class TestEntity {
    private String name;
 
    private Integer age;
 
    private final String type = "person";
 
    @SneakyThrows
    public  void  outputStream(){
         @Cleanup OutputStream outputStream = new FileOutputStream(new File("/Users/hello"));
    }
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public void outputStream() {
        try {
            FileOutputStream $ex = new FileOutputStream(new File("/Users/hello"));
            if(Collections.singletonList($ex).get(0) != null) {
                $ex.close();
            }
 
        } catch (Throwable var2) {
            throw var2;
        }
    }
}

@async注解
启动加上@EnableAsync,需要执行异步方法加入@Async
在这里插入图片描述
在这里插入图片描述

<think>好的,我现在需要回答用户关于Java常用注解及其用法的问题。首先,用户已经提供了两段考引用,我需要结合这些信息来构建回答。让我仔细看看用户提供的引用内容。 引用[1]提到Java注解是JDK5引入的新特性,用于为代码提供元数据,将元数据与程序元素关联,有助于简化程序设计。这部分解释了注解的基本概念和作用,可以作为引言部分的基础。 引用[2]则给出了一些具体的常用注解,比如在业务层的方法、类、接口上使用的注解,特别是事务管理相关的注解,比如可能指的是Spring的@Transactional。这里需要确认@Transactional的作用,以及它在Spring中的使用场景。 用户的需求是了解常用注解列表及作用,所以我需要列举不同类别下的常见注解,并说明它们的作用和用法。可能需要分层次来组织,比如分为核心Java注解、Spring框架中的注解、JUnit测试注解等,这样结构更清晰。 接下来,我需要回忆或查找常见的Java注解。核心Java中的内置注解包括@Override、@Deprecated、@SuppressWarnings,这些必须提到。然后是框架中常用的,比如Spring的@Autowired、@Controller、@Service、@Repository,JPA的@Entity、@Table,Spring Boot的@SpringBootApplication,Spring MVC的@RequestMapping及其变体如@GetMapping,还有Spring的@Transactional。JUnit的@Test、@Before、@After等也是常用的。 需要确保每个注解的作用和使用场景都准确。比如@Transactional用于事务管理,需要说明它可以用在方法或类上,开启事务、提交或回滚的条件。@Autowired用于自动注入依赖,@Component及其派生注解用于标记Spring管理的组件。 另外,用户提供的引用[2]中提到了事务管理,这可能涉及Spring的声明式事务管理,需要强调@Transactional的使用位置和效果。同时,用户可能对如何正确使用这些注解感兴趣,所以需要给出简单的示例,如代码片段,但根据系统指令,行内代码需要用$...$格式,独立代码块用$$...$$,但用户示例中的Python代码用了```python,这里可能需要调整。不过用户的问题是关于Java的,所以可能需要用Java代码示例,但根据指令,数学表达式才需要LaTeX,代码可能用代码块。不过原问题中的示例用户可见层允许使用代码块,所以可能需要用正确的格式。 还需要注意系统级指令中的要求:行内数学表达式用$...$,独立公式用$$...$$,但这里的回答主要是文本和代码,可能不涉及数学公式,但需要正确使用LaTeX格式。不过用户的问题是关于注解,可能不需要数学表达式,但需要确保所有提到的代码部分正确格式化。 另外,需要生成相关问题,用户示例中列出了三个相关问题,我需要根据回答内容提出相关的扩展问题,比如注解的工作原理、自定义注解的方法、事务注解的注意事项等。 现在,我需要组织这些信息,按照用户提供的结构,首先回答常用注解列表及作用,分点说明,然后生成相关问题。同时,确保引用标记正确添加,引用[1]和引用[2]分别在相应的位置被引用。 需要验证每个注解的准确性,比如@Transactional是否属于Spring而非Java核心库,确保分类正确。例如,核心Java注解是Java自带的,而其他如Spring、JPA、JUnit的注解属于特定框架的扩展。 可能还需要补充一些常用注解,比如Lombok的@Data、@Getter、@Setter,但用户提供的引用中没有提到,是否需要包含?根据用户的问题,是询问Java中的常用注解,可能包括核心和主流框架的,所以可以包括Spring相关的,但Lombok可能属于第三方库,视情况是否提及。不过用户提供的引用中没有提到,可能暂时不包含,保持回答基于用户提供的资料和常见知识。 总结,结构可能如下: 1. 引言:Java注解的作用(引用[1]) 2. 分类列举常用注解: a. Java核心注解:@Override, @Deprecated, @SuppressWarnings b. Spring框架注解:@Autowired, @Component, @Controller, @Service, @Repository, @Transactional(引用[2]) c. Spring MVC注解:@RequestMapping, @GetMapping等 d. JPA注解:@Entity, @Table e. JUnit注解:@Test, @Before, @After f. Spring Boot注解:@SpringBootApplication 3. 每个注解的作用和示例代码(如果有必要) 4. 相关问题:如何自定义注解注解的工作原理、事务管理注意事项等 需要确保每个部分的信息准确,引用正确。例如,在提到@Transactional时,引用[2]中的内容需要被引用,说明其作用和在业务层的应用。 最后,生成的相关问题应基于回答内容,帮助用户进一步探索。</think>### Java常用注解列表及作用 Java注解(Annotation)是JDK5引入的元数据标记机制,用于为类、方法、字段等元素添加说明信息,简化代码与元数据的耦合[^1]。以下是Java中常见的注解及其作用: --- #### 1. **核心Java注解** - **`@Override`** 标记方法重写父类或接口的方法,编译器会检查方法签名是否正确。 ```java @Override public String toString() { return "example"; } ``` - **`@Deprecated`** 标记已过时的方法或类,调用时编译器会生成警告。 ```java @Deprecated public void oldMethod() { /* ... */ } ``` - **`@SuppressWarnings`** 抑制编译器警告(如未使用变量、未检查类型等)。 ```java @SuppressWarnings("unchecked") List list = new ArrayList(); ``` --- #### 2. **Spring框架注解** - **`@Autowired`** 自动注入依赖对象,默认按类型匹配。 ```java @Autowired private UserService userService; ``` - **`@Component`及其派生注解** 标记类为Spring管理的组件: - **`@Controller`**:Web层控制器。 - **`@Service`**:业务逻辑层。 - **`@Repository`**:数据访问层。 ```java @Service public class UserService { /* ... */ } ``` - **`@Transactional`** 声明事务管理,方法执行前开启事务,成功提交,异常回滚[^2]。 ```java @Transactional public void updateUser(User user) { /* ... */ } ``` --- #### 3. **Spring MVC注解** - **`@RequestMapping`及变体** 定义HTTP请求映射: - `@GetMapping`、`@PostMapping`:限定请求方法。 ```java @GetMapping("/users") public List<User> getUsers() { /* ... */ } ``` - **`@ResponseBody`** 将方法返回值直接写入HTTP响应体(如JSON)。 --- #### 4. **JPA(数据库映射)注解** - **`@Entity`** 标记类为数据库实体,对应一张表。 - **`@Table`** 指定实体对应的表名。 ```java @Entity @Table(name = "user") public class User { /* ... */ } ``` --- #### 5. **JUnit测试注解** - **`@Test`** 标记方法为测试用例。 - **`@Before`/`@After`** 在每个测试方法执行前/后运行。 ```java @Before public void setup() { /* ... */ } ``` --- #### 6. **Spring Boot注解** - **`@SpringBootApplication`** 标记主启动类,整合自动配置、组件扫描等功能。 ```java @SpringBootApplication public class Application { public static void main(String[] args) { /* ... */ } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值