What are annotations?
Annotations are meta data in Java.
Annotations are meta data in Java.
What is meta data?
A metadata is data about data. Metadata adds some additional flags on your actual data, and in runtime either you or JVM who understand these flags, can utilize this metadata information to make appropriate decisions based on context.
A metadata is data about data. Metadata adds some additional flags on your actual data, and in runtime either you or JVM who understand these flags, can utilize this metadata information to make appropriate decisions based on context.
How it looks like?
Basic annotation is look like following.
@MyAnnotation
It starts with @ sign followed by the Annotation name.
Annotations are defined as following.
@interface MyAnnotation { }
Basic annotation is look like following.
@MyAnnotation
It starts with @ sign followed by the Annotation name.
Annotations are defined as following.
@interface MyAnnotation { }
I didn’t get it…
Ok, Annotations do basically three things in Java. It provides….
Ok, Annotations do basically three things in Java. It provides….
- Embed instructions to Java Compiler
- Embed instructions to Source Code Processing Tools such as IDEs
- Embed metadata which can be read at runtime by your Java Application or Third Party Tools
Lets look at some simple example
Below example shows how @Override annotation checks that the annotated method is overridden method.
Below example shows how @Override annotation checks that the annotated method is overridden method.
public class DemoClass
{
//some code
@Override
public String toString()
{
return super.toString();
}
@Override
public int hashCode()
{
return super.hashCode();
}
}
It causes a compile time “error” if the annotated method toString() is not found in one of the parent classes or implemented interfaces.
If someone deleted the method from parent class we now know in compilation instead of runtime.
Cool right?
If someone deleted the method from parent class we now know in compilation instead of runtime.
Cool right?
Only this?
Noo, Implementations of Annotations are countless. Frameworks like Spring/Strut uses annotations to make sure we get a clean and quality output.
Java has built in Annotations which fulfills different instructions
@Override
@Deprecated
@SuppressWarnings
@SafeVarargs
@FunctionalInterface
You can always get the latest predefined Java annotations here.
Noo, Implementations of Annotations are countless. Frameworks like Spring/Strut uses annotations to make sure we get a clean and quality output.
Java has built in Annotations which fulfills different instructions
@Override
@Deprecated
@SuppressWarnings
@SafeVarargs
@FunctionalInterface
You can always get the latest predefined Java annotations here.
So how to defined our own Custom Annotations?All Annotations must be defined with @interface keyword.
And usage be as follows
public @interface DemoAnnotation {}
And usage be as follows
@DemoAnnotation
public void toggle() {}
It’s complicated to me…
Ok, Let’s get a real world example from Spring framework.
Spring has an Annotation named @Lazy.
Ok, Let’s get a real world example from Spring framework.
Spring has an Annotation named @Lazy.
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Lazy {
/**
* Whether lazy initialization should occur.
*/
boolean value() default true;
}
@Lazy
class VehicleFactoryConfig {
@Lazy(false)
Engine engine() {
return new Engine();
}
}
We can use the @Lazy when we want to initialize our bean lazily.
By default, Spring creates all singleton beans eagerly at the startup of the application context.
However, there are cases when we need to create a bean when we request it, but not at application startup.
In this case we can use @Lazy annotation to mark the fields that should/ should not initialize at startup.
By default @Lazy is marked as true. But if wanted we can make it false for specific method or a bean.
Easy as that….
Now you have a basic understanding of Annotations.
Let me know below if any questions. Cheers!
By default, Spring creates all singleton beans eagerly at the startup of the application context.
However, there are cases when we need to create a bean when we request it, but not at application startup.
In this case we can use @Lazy annotation to mark the fields that should/ should not initialize at startup.
By default @Lazy is marked as true. But if wanted we can make it false for specific method or a bean.
Easy as that….
Now you have a basic understanding of Annotations.
Let me know below if any questions. Cheers!