Technical architectural analysis, practical configuration manifests, and deployment instructions for Spring Lombok Annotations: Production Usage.
Common
Annotation
If you add the @Data annotation to a class, you can use highly useful methods such as toString().
@Data //์๋์ ๋ชจ๋ ์ ๋
ธํ
์ด์
์ ํฌํจํ๋ ๋ฌธ๋ฒ
// @Getter + @Setter + @ToString + @EqualsAndHashCode + @RequiredArgsConstructor
@NoArgsConstructor //ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ๊ธฐ๋ณธ์์ฑ์ ์๋ ์ ์ธ
@AllArgsConstructor //๋ชจ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ช
์ํ๋ ๊ธฐ๋ณธ์์ฑ์ ์๋ ์ ์ธ
@RequiredArgsConstructor //final์ด๋ @NonNull์ธ ํ๋ ๊ฐ๋ง ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋ ์์ฑ์ ๋ง๋ฆ
Method
public class DataExample {
private final String name;
private int age;
private double score;
private String[] tags;
public DataExample(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setScore(double score) {
this.score = score;
}
public double getScore() {
return this.score;
}
public String[] getTags() {
return this.tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
@Override public String toString() {
return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";
}
protected boolean canEqual(Object other) {
return other instanceof DataExample;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof DataExample)) return false;
DataExample other = (DataExample) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
if (this.getAge() != other.getAge()) return false;
if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.getScore());
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + this.getAge();
result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
return result;
}
public static class Exercise<T> {
private final String name;
private final T value;
private Exercise(String name, T value) {
this.name = name;
this.value = value;
}
public static <T> Exercise<T> of(String name, T value) {
return new Exercise<T>(name, value);
}
public String getName() {
return this.name;
}
public T getValue() {
return this.value;
}
@Override public String toString() {
return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";
}
protected boolean canEqual(Object other) {
return other instanceof Exercise;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Exercise)) return false;
Exercise<?> other = (Exercise<?>) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());
return result;
}
}
}
Feature
@RequiredArgsConstructor: Automate dependency injection when implementing interfaces
GPT
> ๐ก
\[Question\]
@RequiredArgsConstructor
public class JobService \{
private final Job calculateServiceInstanceRunningTimeJob;
private final Job serviceInstanceStateCheckJob;
If the service is defined this way, dependency injection is done by default
public class CalculateServiceInstanceRunningTimeJob implements Assigns the Job class to calculateServiceInstanceRunningTimeJob,
public class ServiceInstanceStateCheckJob implements Can I assume that the Job class will be assigned to serviceInstanceStateCheckJob?
\[Answer\]
Yes, that’s right.ย In a class to which @RequiredArgsConstructor is applied, a constructor that receives the final field of the class as a parameter is automatically created. This constructor assigns a value to the field according to the injected parameters.
So, for example, if you have code like this:
javaCopy code
@RequiredArgsConstructor
public class JobService {
private final Job calculateServiceInstanceRunningTimeJob;
private final Job serviceInstanceStateCheckJob;
// ...
}
The JobService class registered as a Spring bean receives these two fields injected through the constructor. At this time, Spring looks at the type of each field, finds the appropriate bean, and injects it. Therefore, the calculateServiceInstanceRunningTimeJob field contains an instance of the CalculateServiceInstanceRunningTimeJob class, and the serviceInstanceStateCheckJob field contains an instance of the ServiceInstanceStateCheckJob class. It will be injected.
โ Implications
โ Default Qualifier when registering a bean
โ Class name variable name (starts with lowercase letter)
โ (Manual) Bean registration
โ @Component
โ (Manual) Service Constructor
@Autowired
public MyService(@Qualifier("implementationA") MyInterface implementationA,
@Qualifier("implementationB") MyInterface implementationB) {
this.implementationA = implementationA;
this.implementationB = implementationB;
}




Leave a Reply