This website uses cookies to ensure you get the best experience on our website.

Skip to content
LOGO
  • Company

    About IGNEK

    Learn about our story and our mission.

    Careers

    We're hiring!

    We always looking for talented peoples. Join our team!

    Blogs

    The latest industry news, updates and info.

    Case Studies

    Take a closer look at the projects we've delivered

    Webinar

    Watch our latest organized webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Custom Enterprise Portal Development for flawless Solutions

    Dedicated Development Team

    Expand Teams, Optimize Development with Our Expertise

    Back-end Development

    Empowering your digital presence with robust backend development expertise

    Front-end Development

    Crafting engaging user experiences through expert frontend development

    Digital Transformation

    Embrace digital transformation by modernizing process

    SaaS Development

    Transform your business with SaaS Innovative Solutions

    Technologies

    Liferay Services

    Development & Customization

    Theme

    Upgradation

    Migration

    Proof of Concept

    Performance Tunning

    Architecture

    Support & Maintenance

    E-commerce

    Expert Advice

    React Services

    Enterprise Development

    Custom Application

    Single Page Application

    Architecture

    API Integration

    Migration

    Consulting

    Maintenance & Support

    Spring Boot Services

    Web Service

    MVC Web Development

    API Integration

    Security

    Migration

    Microservices

    Support & Maintenance

    AEM Services

    Consulting Service

    Site Development

    Migration & Upgradation

    Integration

    Maintenance & Support

    Custom Solutions

    Content Management

    Team Augementation

  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Transforming industries through cutting edge SaaS solutions.

    Customer Portal : Boost Business Productivity

    Streamline operations and enhance productivity with our Customer Portal solution.

    Liferay Based Intranet Portal

    Internal portal for company communication, collaboration and resources

    Integration

    Matomo Integration with Liferay

    Integrate Matomo to enable user interactions seamlessly.

    Microsoft Teams integration with Liferay

    Integrate to enable seamless collaboration, messaging within your portal.

    Jira Integration With Liferay

    Optimizes business operations by integrating with Jira.

  • Hire Us

    Liferay

    Get expert Liferay developers for seamless portal solutions.

    React JS

    Get expert ReactJS developers for dynamic web solutions.

    Web Developer

    Get custom web solutions from IGNEK's expert developers.

    Spring Boot

    Get top-notch Spring Boot developers for your project success!

    Healthcare

    Get top Healthcare Developers for cutting-edge medical software.

    MERN Stack

    Get expert MERN developers from IGNEK for custom, innovative project solutions.

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • info@ignek.com
    • Ahmedabad, Gujarat, India – 382470
SCHEDULE CALL
  • COMPANY
    • About
    • Case Studies
    • Blogs
    • Career
    • Webinar
  • SERVICES
    • Enterprise Portal Development
    • Dedicated Development Team
    • Back-end Development
    • Front-end Development
    • Digital Transformation
    • SaaS Development
  • SOLUTION
    • Customer Portal:  Boost Business Productivity
    • SaaS : Software as a Service
    • Liferay Based Employee Intranet Portal
  • TECHNOLOGY
    • Liferay Services
      • Liferay Development and Customization
      • Liferay Theme Development Service
      • Liferay DXP 7.4 Upgrade
      • Liferay Migration
      • Liferay Proof Of Concept
      • Liferay Performance Tuning
      • Liferay Architecture Design Service
      • Liferay Support And Maintenance Service
      • Liferay Ecommerce Development
      • Liferay Expert Advice
    • ReactJS Services
      • ReactJS Enterprise Application Development
      • ReactJS Custom Application Development
      • ReactJS Single Page Application Development (SPA)
      • ReactJS Applications Architecture
      • ReactJS API Integration
      • ReactJS Migration
      • ReactJS Consulting
      • ReactJS Maintenance and Support
    • Spring Boot Services
      • Spring Boot Web Development Service
      • Spring MVC Web Development
      • Spring Boot API Integration Service
      • Spring Boot Security
      • Spring Boot Migration Service
      • Spring Boot Microservices
      • Spring Boot Support & Maintenance Service
    • AEM Development Services
      • AEM Consulting Services
      • AEM Site Development
      • AEM Migration & Upgradation
      • AEM Integration Services
      • AEM Maintenance & Support
      • AEM Content Management
      • Custom AEM Solutions
      • AEM Team Augmentation
  • INTEGRATION
  • HIRE US
    • Hire Liferay Developer
    • Hire ReactJS Developer
    • Hire Spring Boot Developer
    • Hire Healthcare Developer
    • Hire Web Developer
    • Hire MERN Stack Developer
  • CONTACT US
  • Company

    About IGNEK

    Careers

    We're hiring!

    Blogs

    Case Studies

    Webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Dedicated Development Team

    Back-end Development

    Front-end Development

    Digital Transformation

    SaaS Development

    Technologies

    Liferay Liferay Services
    • Development & Customization
    • Theme
    • Upgradation
    • Migration
    • Proof of Concept
    • Performance Tuning
    • Architecture
    • Support & Maintenance
    • E-commerce
    • Expert Advice
    React React Services
    • Enterprise Development
    • Custom Application
    • Single Page Application
    • Architecture
    • API Integration
    • Migration
    • Consulting
    • Maintenance & Support
    Spring Boot Spring Boot Services
    • Web Service
    • MVC Web Development
    • API Integration
    • Security
    • Migration
    • Microservices
    • Support & Maintenance
    AEM AEM Services
    • Consulting Service
    • Site Development
    • Migration & Upgradation
    • Integration
    • Maintenance & Support
    • Custom Solutions
    • Content Management
    • Team Augmentation
  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Customer Portal : Boost Business Productivity

    Liferay Based Intranet Portal

    Integration

    Matomo Integration with Liferay

    Microsoft Teams integration with Liferay

    Jira Integration With Liferay

  • Hire Us

    Liferay

    React JS

    Web Developer

    Spring Boot

    Healthcare

    MERN Stack

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • IGNEK
    • Ahmedabad, Gujarat, India – 382470
  • SCHEDULE CALL

Custom Annotations in Spring Boot

  • Spring Boot
  • October 17, 2023

Share On :

Annotation
  • Annotation represents the additional information of the program or provides the metadata of the program.
  • We can add annotations on classes, methods, interfaces, packages, etc. After adding the annotation it cannot affect the source program.
  • Two types of annotations:
    • Inbuilt Annotations: Defined by the system itself.
    • Custom Annotations: Defined by user or client.
Custom Annotation
  • Custom Annotation means the user or client creates the annotations by themselves and uses them very effectively in the program.
  • Types of Custom Annotations
    1. Marker Annotation (Without declaring any method)
    2. Single-Valued Annotation (With single method)
    3. Multi-Valued Annotation (With multiple methods or more than one method)

1)  Marker Annotation:

				
					public @interface class_name{

}

				
			
  • This is the Marker Annotation. In Marker Annotation, there is nothing to declare any method.

2) Single Valued Annotation:

				
					public @interface class_name{
	data_type method_name();
}

				
			

3) Multi-Valued Annotation:

				
					public @interface class_name{
	data_type method_name();
	data_type method_name2();
	data_type method_name3();
	……
	……
}

				
			
Ways of Creating Custom Annotations
  • There are three ways to create custom annotations:
    1. Class Level Annotation
    2. Field Level Annotation 
    3. Method Level Annotation

1)  Class Level Annotation:

  • When we declare the Class-Level Annotation by using the @Interface annotation.
				
					public @interface class_name{

}

				
			
  • The next step is to provide the @Target and the scope of the annotation which means adding the @Retention of the annotation.
				
					@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface class_name{

}

				
			
  • @Target and @Retention annotations are called meta-annotation.
  • Here, declare the @Target annotation indicates the custom annotation that can be used for annotating, and in this annotation, we declare various types of ElementType like,

 

Element TypeDescription
TYPEYou can use this ElementType on the class level, interface level, or on Enumeration.
FIELDYou can use this ElementType on the field level.
METHODYou can use this ElementType on the method level.
CONSTRUCTORYou can use this ElementType on the constructor level.
LOCAL_VARIABLEYou can use this ElementType on the local variable.
PACKAGEYou can use this ElementType on the package level.
ANNOTATION_TYPEYou can use this ElementType on the package level.
TYPE_PARAMETERYou can use this ElementType on the type parameter.
PARAMETERYou can use this ElementType on the formal parameter. 
  • You can also declare multiple annotations in ElementType like,
				
					@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface class_name{

}

				
			
  • Here, declare the @Retention annotation specifies the scope of the annotation and in this annotation, we declare various RetentionPolicy like
    1. RUNTIME: This Retention Policy indicates that the annotation is available at runtime or we can use our annotation on runtime.
    2. CLASS:  It is a default Retention Policy in Java. This Retention Policy is used in the .class file and discarded on RUNTIME.
    3. SOURCE: This SOURCE Retention policy is also discarded at RUNTIME.

2) Field Level Annotation:

  • We are annotating the field by using the same syntax as in Elementype but changing the type FIELD.
				
					@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface class_name{
	String key() default “ ”;
}

				
			

3) Method Level Annotation:

    • We annotate the method. This method-level annotation initializes the object at the method level and indicates that this annotation identifies or marks it as a method by using @Target annotation.
				
					@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface class_name{
}
				
			

There are some other annotations like, @Inherited and @Documented.

  • @Inherited annotation is used when SubClass extends the SuperClass and SubClass uses the same annotation that SuperClass is used at that time @Inherited annotation is used.
  • Example: There is “A” as a SuperClass and “B” as a SubClass. Now B used the same annotation that A can use.
				
					@MyAnno(Parameters)
public class A{
}

				
			
				
					@MyAnno(Parameters)
public class B{
}

				
			
				
					@Inherited
public @interface MyAnno{
}

				
			
  • We have to declare the @Inherited annotation when we are creating the Custom Annotation.
  • @Documented annotation is meta-annotation. This tag will be used after when our software is ready. When we are declaring the custom annotation without using @Documented annotation the description of the annotation will not show on the Javadoc tool (On Java Document). 
				
					//By without using @Documented annotation
public @interface class_name{

}

				
			
				
					//With using @Documented annotation
@Documented  @interface class_name{

}

				
			
  • After using the @Documented tag, we are showing all the descriptions with the tag which are showing on the Javadoc tool(On Java Document).
				
					//With using @Documented annotation
@Documented  
@interface class_name{

}

				
			

Example of Custom Annotation

  • Step 1: Create a Springboot project using Spring initializr.
  • Step 2:Choose the Project field as Maven, language as Java, Springboot version,  give the name to the project, and add dependencies like Spring Web and Springboot dev tools.
How to initialize a Spring Project?
  • Step 3: In the src/main/java directory, create an interface (here, SmartPhone). After creating the interface,  declare  ”@”  before the interface i.e.  “@interface”.
				
					//SmartPhone.java
package com.ignek.customanno.customannotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SmartPhone {
	String os() default "Symbian";
	int version() default 1; 
}
				
			
    • We declare EntityType as TYPE, so we can use this annotation at the class level, interface level, and enumeration.
    • Here, RetentionPolicy is a RUNTIME annotation available at runtime.
    • We declare two methods os() and version() and also declare the default value. If we do not declare any explicit value, the value returned by these methods will be considered as a default value.
  • Step 4:  In the src/main/java directory, create one class(here, NokiaSeries) and assign custom annotation @SmartPhone.
				
					// NokiaSeries.java
package com.ignek.customanno.customannotation;

@SmartPhone(os="Android", version=6)
public class NokiaSeries {
	String os;
	int version;
	public NokiaSeries(String os, int version) {
		super();
		this.os = os;
		this.version = version;
	}
	
}
				
			
    • Assign custom annotation @SmartPhone and into the parentheses pass the parameter explicitly. If we do not assign a parameter to the parentheses, it is called the default value.
  • Step 5: In the Main file.
				
					// CustomannotationApplication.java
package com.ignek.customanno.customannotation;

import java.lang.annotation.Annotation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CustomannotationApplication {

	public static void main(String[] args) {
		SpringApplication.run(CustomannotationApplication.class, args);
		
		NokiaSeries ns = new NokiaSeries("Fire", 5);
		Class c = ns.getClass();
		Annotation an = c.getAnnotation(SmartPhone.class);
		SmartPhone s = (SmartPhone) an;
		System.out.println(s.os());
		System.out.println(s.version());
	}
}
				
			
    • We declare the object of the NokiaSeries class and pass the argument and test which values will be called.
    • After creating the object of the NokiaSeries class, we get the class by using that class object and the getClass() method.
    • And then get the custom annotation interface by using the getAnnotation() method.
  • Step 6: Create the project structure like this:
Project Structure with file directory In Spring Boot
Output:
Terminal result snapshot

Explore Our Services

Discover how we can help your business thrive, whether you’re running a small startup, an SME, or a large enterprise. We’re here to understand your unique needs and goals, offering the expertise and resources to support your journey to success.

Stay informed about our Spring Boot services and updates by subscribing to our newsletter—just fill in the details below to subscribe.

Loading
Loading...

Related Blogs

August 28, 2023
Best Approach to integrate Spring Boot with GraphQl
GraphQL with Spring Boot
October 17, 2023
Apache Kafka integration: Pros and cons
Apache Kafka Case Studies: Pros and cons
Loading...

Featured Projects Portfolios

October 6, 2023
Dynamic E-commerce Platform_for Sales, Rentals and Inventory Management
Dynamic E-commerce Platform for sales, Rentals and Inventory Management
October 4, 2023
Invoice-Based Lending Finance Portal _ Fast, Secure, Reliable
Invoice-Based Lending Finance Portal : Fast, Secure, Reliable

Digital Solutions for Your Business with IGNEK

4.9

5.0

5.0

5.0

LOGO

Making the world a better place through constructing elegant hierarchies

COMPANY

  • About
  • Career
  • Case Study
  • Blogs

SERVICES

  • Enterprise Portal Development
  • Dedicated Development Team
  • Back-end Development
  • Front-end Development
  • Digital Transformation
  • SaaS Development

HIRE US

  • Liferay
  • Spring Boot
  • ReactJS
  • Healthcare
  • MERN Stack
  • AEM

CONTACT

  • info@ignek.com
  • info@ignek.com
SALES
  • sales@ignek.com
  • (+91) 635 157 6580
CAREER
  • hr@ignek.com
  • (+91) 932 849 5160
  • E 910-912, Ganesh Glory 11, Jagatpur Road, SG Highway, Ahmedabad, Gujarat - 382470

© 2018-2025 IGNEK, Inc. All rights reserved

Linkedin Facebook X-twitter Instagram