INTRODUCTION
We have learned Spring boot annotations and how they helped to reduce the time of developer without creating the xml configuration. Today we are learning custom annotations and how custom annotations are created and how we can use those annotations in our projects.
How to create Custom Annotations ?
We are creating annotation at level
- Class level annotation.
- Field level annotation.
- Method level annotation.
STEP 1 : Create a Custom Annotation using @interface keyword.
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UserAuthentication{
}
1) @Documented : @Documented is a meta-annotation. The purpose of the @Documented is to indicate that an annotation should be included in the generated JavaDoc documentation. By default , when you create a custom annotation JavaDoc documentation even if you are using it in your code. But if you want the generated annotation should be visible in JavaDoc then use @Documented meta annotation.
2) @Inherited : @Inherited is also a meta-annotation. @Inherited annotation indicate that the custom annotation should be inherited by subclasses if it is applied to a superclass. the @Inherited meta-annotation only affects class-level annotations. It does not have any impact on method-level or field-level annotations.
3) @Target : @Target annotation defines where to apply the annotation. In our case it is added at method level. We can use as FIELD, TYPE and CONSTRUCTOR.
Field level annotation :
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface UserAuthentication{
}
Class level annotation :
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface UserAuthentication{
}
4) @Retention : @Rentention annotation defines when to apply this annotation. It can be RUNTIME, SOURCE and CLASS. In our case , It is RUNTIME.
STEP 2 : Create a Aspect.
Aspect oriented programming is used to implement the annotations.
Example :
@Aspect
@Component
public class AuthenticationofUser {
@Around("@annotation(UserAuthentication)")
public Object authenticate(ProceedingJoinPoint joinPoint) throws Throwable {
// before execution of method
String user_name = (Integer) joinPoint.getArgs()[0];
System.out.println("User Name: " + user_name);
if (!user_name.equals("suraj")) {
//write authorization verification business logic
System.out.println("Invalid User added : " + user_name);
return user_name + " is Invalid User. Please login with correct credential.";
}
// actual method is invoked
Object result = joinPoint.proceed();
System.out.println(result);
return result;
}
}
1) @Aspect : @Aspect annotation provide class is an Aspect class.
2) @Component : Class is a Spring Bean.
3) @Around with @annotation : Pass the Custom annotation created inside the @Around annotation and use that annotation where you want to execute your logic.
4) ProceedingJointPoint : It is used to accept the input parameter provided by rest API.
5) jointPoint.proceed : It used to execute the actual method.
STEP 3 : Add the annotation.
@RestController
public class UserController {
@Getmapping(/api/login)
@UserAuthentication
public String loginWebPortal(@RequestParam String user_name , HttpServletRequest servletRequest)
return "successfully logged in with " + user_name.;
}
Scenarios :
1) When you run the application with invalid username
Response :
Anvee is invalid user. Please login with correct credential.
2) When you login with Valid username
Response :
successfully logged in with Suraj.
You can also use custom annotation at field level.
STEP 1 : create field level custom annotation.
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DatabaseField{
String name();
class<?> type();
}
STEP 2 : Add Annotation.
@Data
@NoArgsConstructor
@AllArgsConstructor
Public class Employee{
@DatabaseField(name = "employeeName", type = String.class)
String name;
Long salary;
}
SUMMARY :
So we can create our own annotations and use at different level in your source code. As we have seen custom annotations for user-authentication. So we can use custom annotations for logging, monitoring or profiling the application.
0 Comments
Post a Comment