Common bean annotations:
@Configuration
@ComponentScan(basePackages = "com.dsam.annotations")
@ComponentScan(basePackageClasses = TheMainFunc.class)
class TheMainFunc {}
ComponentScan scans the packages for classes. We can define classes/packeges by using
arguments.
@Component
class CarUtility {
// ...
}
@Component is a class level annotation. During the component scan, Spring Framework
automatically detects classes annotated with @Component.
@Repository
class VehicleRepository {
// ...
}
DAO or Repository classes usually represent the database access layer in an application, and
should be annotated with @Repository:
@Service
public class VehicleService {
// ...
}
The business logic of an application usually resides within the service layer, so we’ll use the
@Service annotation to indicate that a class belongs to that layer.
@Controller
public class VehicleController {
// ...
}
@Controller is a class level annotation, which tells the Spring Framework that this class serves
as a controller in Spring MVC.
@AutoWired
The Spring framework enables automatic dependency injection. In other words, by declaring all
the bean dependencies in a Spring configuration file, Spring container can autowire
relationships between collaborating beans. This is called Spring bean autowiring.
Request Mapping
@RequestMapping(value="add-product")
To define the request URL
@GetMapping
@PostMapping
@PutMapping
@PatchMapping
@DeleteMapping
@GetMapping(value = "login") define mapping with url
Entity/Database/Model/JPA
@Data
//Generates getters for all fields, a useful toString method, and hashCode and equals
implementations that check all non-transient fields. Will also generate setters for all non-final fields,
as well as a constructor.
@NoArgsConstructor
//will generate a constructor with no parameters. If this is not possible (because of final fields), a
compiler error will result instead, unless @NoArgsConstructor(force = true) is used, then all final
fields are initialized with 0 / false / null.
@AllArgsConstructor
@AllArgsConstructor generates a constructor with 1 parameter for each field in your class.
Fields marked with @NonNull result in null checks on those parameters.
@Entity
@Table(name = "order")
Before the class
@Id //Define primary key
@GeneratedValue
@Column(name = "order_id")
private String orderId;
@GeneratedValue(strategy=SEQUENCE, generator="ID_SEQ") //This annotation specifies the
generation strategies for the values of primary keys.
@OrderBy("id asc")
private Set employee_address; // sort all addresses by their id
@OneToOne(cascade = CascadeType.MERGE)
@PrimaryKeyJoinColumn
private EmployeeDetail employeeDetail;
@ManyToOne
@JoinColumn(name = "statusId")
private EmployeeStatus status;
@OneToMany(mappedBy = "employee", fetch = FetchType.EAGER)
@OrderBy("firstName asc")
private Set communications;
For Validation:
Dependencies :
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
Annotations:
@NotNull(message = "Name cannot be null")
private String name;
@AssertTrue
private boolean working;
@Size(min = 10, max = 200, message
= "About Me must be between 10 and 200 characters")
private String aboutMe;
@Min(value = 18, message = "Age should not be less than 18")
@Max(value = 150, message = "Age should not be greater than 150")
private int age;
@Email(message = "Email should be valid")
private String email;
● @NotEmpty validates that the property is not null or empty; can be
applied to String, Collection, Map or Array values.
● @NotBlank can be applied only to text values and validates that the
property is not null or whitespace.
● @Positive and @PositiveOrZero apply to numeric values and validate
that they are strictly positive, or positive including 0.
● @Negative and @NegativeOrZero apply to numeric values and
validate that they are strictly negative, or negative including 0.
● @Past and @PastOrPresent validate that a date value is in the past or
the past including the present; can be applied to date types including
those added in Java 8.
● @Future and @FutureOrPresent validate that a date value is in the
future, or in the future including the present.
@Positive(message = "Can't be negative")
Deploy a Spring boot App to Google App Engine
1. Create a project on Google App Engine
2. Setup app engine application
3. Define package type “war” in pom.xml
<packaging>war</packaging>
0. Define dependency in your spring boot project (pom.xml)
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<projectId>snappy-cistern-380614</projectId>
<version>GCLOUD_CONFIG</version>
</configuration>
</plugin>
0. Create main/appengine/app.yaml file
0. Run “mvn package appengine:run” on terminal
0. Install google code plugin on intelliJ
0. Configure google cloud option
0. Deploy the app