TheRiver | blog

You have reached the world's edge, none but devils play past here

0%

java and spring中的注解

因为用到了spring boot在开发,所以开始正式学习java语法和生态。这一篇是关于注解的。

spring

@Component @Service @Repository @Control @RestControl

https://www.baeldung.com/spring-component-repository-service

https://www.baeldung.com/spring-bean-annotations

inject

@AutoWired @Resource @Inject

https://stackoverflow.com/questions/4093504/resource-vs-autowired

这篇文章很不错,读完就懂了:Wiring in Spring: @Autowired, @Resource and @Inject

lombok

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

@Data

All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor!

https://projectlombok.org/features/Data

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

https://projectlombok.org/features/constructor

@Builder

https://projectlombok.org/features/Builder

javadoc也写的挺详细的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
The builder annotation creates a so-called 'builder' aspect to the class that is annotated or the class that contains a member which is annotated with @Builder.
If a member is annotated, it must be either a constructor or a method. If a class is annotated, then a package-private constructor is generated with all fields as arguments (as if @AllArgsConstructor(access = AccessLevel.PACKAGE) is present on the class), and it is as if this constructor has been annotated with @Builder instead. Note that this constructor is only generated if you haven't written any constructors and also haven't added any explicit @XArgsConstructor annotations. In those cases, lombok will assume an all-args constructor is present and generate code that uses it; this means you'd get a compiler error if this constructor is not present.
The effect of @Builder is that an inner class is generated named TBuilder, with a private constructor. Instances of TBuilder are made with the method named builder() which is also generated for you in the class itself (not in the builder class).
The TBuilder class contains 1 method for each parameter of the annotated constructor / method (each field, when annotating a class), which returns the builder itself. The builder also has a build() method which returns a completed instance of the original type, created by passing all parameters as set via the various other methods in the builder to the constructor or method that was annotated with @Builder. The return type of this method will be the same as the relevant class, unless a method has been annotated, in which case it'll be equal to the return type of that method.
Complete documentation is found at the project lombok features page for @Builder .
Before:
@Builder
class Example<T> {
private T foo;
private final String bar;
}

After:
class Example<T> {
private T foo;
private final String bar;

private Example(T foo, String bar) {
this.foo = foo;
this.bar = bar;
}

public static <T> ExampleBuilder<T> builder() {
return new ExampleBuilder<T>();
}

public static class ExampleBuilder<T> {
private T foo;
private String bar;

private ExampleBuilder() {}

public ExampleBuilder foo(T foo) {
this.foo = foo;
return this;
}

public ExampleBuilder bar(String bar) {
this.bar = bar;
return this;
}

@java.lang.Override public String toString() {
return "ExampleBuilder(foo = " + foo + ", bar = " + bar + ")";
}

public Example build() {
return new Example(foo, bar);
}
}
}

summary

其他的遇到了再补充,找到合适的资料就贴上来,尽量看上游的资料。

reference

https://projectlombok.org/

----------- ending -----------