From 5259c3e3251503001b02a540145c59529bf15026 Mon Sep 17 00:00:00 2001 From: Skywalker690 Date: Thu, 18 Sep 2025 20:49:31 +0530 Subject: [PATCH] Add Lombok Dependency to Reduce Boilerplate Code --- pom.xml | 5 +++ .../samples/petclinic/model/BaseEntity.java | 11 +++--- .../samples/petclinic/model/NamedEntity.java | 12 +++---- .../samples/petclinic/model/Person.java | 20 +++-------- .../samples/petclinic/model/Role.java | 19 +++------- .../samples/petclinic/model/User.java | 36 +++---------------- .../security/BasicAuthenticationConfig.java | 3 +- .../petclinic/service/ClinicServiceImpl.java | 2 ++ .../petclinic/service/UserServiceImpl.java | 3 +- 9 files changed, 31 insertions(+), 80 deletions(-) diff --git a/pom.xml b/pom.xml index 0e0b8f06..34306722 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,11 @@ org.springframework.boot spring-boot-starter-jdbc + + org.projectlombok + lombok + true + org.springframework.boot spring-boot-starter-web diff --git a/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java b/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java index 80bf7553..0aecbbb1 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java @@ -21,6 +21,8 @@ import jakarta.persistence.MappedSuperclass; import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Getter; +import lombok.Setter; /** * Simple JavaBean domain object with an id property. Used as a base class for objects needing this property. @@ -28,19 +30,14 @@ * @author Ken Krebs * @author Juergen Hoeller */ +@Setter +@Getter @MappedSuperclass public class BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) protected Integer id; - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } @JsonIgnore public boolean isNew() { return this.id == null; diff --git a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java index 0ae19533..f4262408 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java @@ -19,6 +19,8 @@ import jakarta.persistence.MappedSuperclass; import jakarta.validation.constraints.NotEmpty; +import lombok.Getter; +import lombok.Setter; /** @@ -28,6 +30,8 @@ * @author Ken Krebs * @author Juergen Hoeller */ +@Setter +@Getter @MappedSuperclass public class NamedEntity extends BaseEntity { @@ -35,14 +39,6 @@ public class NamedEntity extends BaseEntity { @NotEmpty private String name; - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - @Override public String toString() { return this.getName(); diff --git a/src/main/java/org/springframework/samples/petclinic/model/Person.java b/src/main/java/org/springframework/samples/petclinic/model/Person.java index 6257f2ee..3fb62b52 100755 --- a/src/main/java/org/springframework/samples/petclinic/model/Person.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Person.java @@ -19,12 +19,16 @@ import jakarta.persistence.MappedSuperclass; import jakarta.validation.constraints.NotEmpty; +import lombok.Getter; +import lombok.Setter; /** * Simple JavaBean domain object representing an person. * * @author Ken Krebs */ +@Setter +@Getter @MappedSuperclass public class Person extends BaseEntity { @@ -36,21 +40,5 @@ public class Person extends BaseEntity { @NotEmpty protected String lastName; - public String getFirstName() { - return this.firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return this.lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Role.java b/src/main/java/org/springframework/samples/petclinic/model/Role.java index da35c8e2..88b4872b 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Role.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Role.java @@ -8,7 +8,11 @@ import jakarta.persistence.UniqueConstraint; import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Getter; +import lombok.Setter; +@Setter +@Getter @Entity @Table(name = "roles" ,uniqueConstraints = @UniqueConstraint(columnNames = {"username", "role"})) public class Role extends BaseEntity { @@ -21,19 +25,4 @@ public class Role extends BaseEntity { @Column( name = "role") private String name; - public User getUser() { - return user; - } - - public void setUser(User user) { - this.user = user; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/User.java b/src/main/java/org/springframework/samples/petclinic/model/User.java index 62d412fa..18d11856 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/User.java +++ b/src/main/java/org/springframework/samples/petclinic/model/User.java @@ -12,7 +12,11 @@ import jakarta.persistence.Table; import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Getter; +import lombok.Setter; +@Setter +@Getter @Entity @Table(name = "users") public class User { @@ -30,38 +34,6 @@ public class User { @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.EAGER) private Set roles; - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public Boolean getEnabled() { - return enabled; - } - - public void setEnabled(Boolean enabled) { - this.enabled = enabled; - } - - public Set getRoles() { - return roles; - } - - public void setRoles(Set roles) { - this.roles = roles; - } - @JsonIgnore public void addRole(String roleName) { if(this.roles == null) { diff --git a/src/main/java/org/springframework/samples/petclinic/security/BasicAuthenticationConfig.java b/src/main/java/org/springframework/samples/petclinic/security/BasicAuthenticationConfig.java index 2d2e6aed..92a83514 100644 --- a/src/main/java/org/springframework/samples/petclinic/security/BasicAuthenticationConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/security/BasicAuthenticationConfig.java @@ -2,6 +2,7 @@ import javax.sql.DataSource; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -17,9 +18,9 @@ @Configuration @EnableGlobalMethodSecurity(prePostEnabled = true) // Enable @PreAuthorize method-level security @ConditionalOnProperty(name = "petclinic.security.enable", havingValue = "true") +@RequiredArgsConstructor public class BasicAuthenticationConfig { - @Autowired private DataSource dataSource; @Bean diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java index 001fb4ae..a86f3975 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Set; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.dao.EmptyResultDataAccessException; @@ -47,6 +48,7 @@ * @author Vitaliy Fedoriv */ @Service +@RequiredArgsConstructor public class ClinicServiceImpl implements ClinicService { private PetRepository petRepository; diff --git a/src/main/java/org/springframework/samples/petclinic/service/UserServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/UserServiceImpl.java index fb099e1e..b495e13f 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/UserServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/UserServiceImpl.java @@ -1,5 +1,6 @@ package org.springframework.samples.petclinic.service; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.User; import org.springframework.samples.petclinic.model.Role; @@ -8,9 +9,9 @@ import org.springframework.transaction.annotation.Transactional; @Service +@RequiredArgsConstructor public class UserServiceImpl implements UserService { - @Autowired private UserRepository userRepository; @Override