[!NOTE] We touched upon Annotations briefly with
@Override. But Annotations govern modern Java frameworks entirely. If you want to use Spring or Hibernate, you must understand exactly how decorators inject massive amounts of hidden logic!
What is an Annotation?
An annotation (the @ symbol followed by a name) is Metadata. It does not perform any physical logic by itself. It is simply a tag stitched onto a class or method.
@Deprecated // Warning! Do not use this method anymore!
public void oldLegacyLogin() { ... }
How Frameworks Use Annotations (Reflection)
If an annotation is just a descriptive label, how does the famous Spring Framework use @RestController to magically launch a Tomcat web server and start mapping HTTP requests?
It uses Reflection.
Java Reflection is an incredibly dangerous, ultra-powerful API that allows the Java JVM to look in the mirror. It lets code inspect itself while it is actively running.
- The Spring Framework starts up.
- It uses Reflection to aggressively scan every single Class in your entire
/srcdirectory. - It asks: "Does this Class have the
@RestControllertag glued to it?" - If Yes, Spring dynamically hijacks the class, generates a massive proxy server block in memory, injects JSON parsing logic, and opens ports to the internet!
// Without you writing ANY server code, Spring detects this tag and boots a server for it via Reflection!
@RestController
public class AuthenticationAPI {
// Spring scans for this tag, and routes HTTP GET requests straight into your method!
@GetMapping("/login")
public String attemptLogin() {
return "You have reached the API!";
}
}
[!CAUTION] Applying Reflection directly yourself (like invoking methods via string names) is extremely slow and breaks compile-time safety. Unless you are literally building the next Spring Boot framework yourself, avoid utilizing the
java.lang.reflectpackage in business logic!