Annotations
Learn how to use annotations in BoxLang for metadata-driven programming with classes, properties, and functions
Annotations in BoxLang are powerful metadata markers that can be attached to classes, properties, and functions. They enable metadata-driven programming, dependency injection, aspect-oriented programming, and runtime reflection capabilities.
🎯 Where Annotations Can Be Used
Annotations can be applied to three main constructs in BoxLang:
Classes - Mark classes with metadata for framework behaviors
Properties - Define metadata for class properties
Functions - Add metadata to methods and functions
📝 Annotation Syntax
BoxLang supports two primary syntax styles for annotations: standalone annotations (using @ prefix) and inline annotations (embedded in the declaration).
Standalone Annotations
Standalone annotations appear on the line before the construct they annotate:
@annotationName
class MyClass {
}
@inject
property userService;
@cached
function getData() {
}Inline Annotations
Inline annotations are embedded directly in the construct declaration:
💡 Annotation Values
Annotations can have different types of values:
No Value (Empty String)
When an annotation has no value, it's defined with an empty string value by default:
String Values
Annotations can have simple string values:
String values can be specified with or without quotes (for simple values):
Boolean Values
Array Literal Values
Annotations can accept array literals:
Struct Literal Values
Struct literals provide named configuration:
Nested Literals
Array and struct literals can be nested:
📋 Annotation Format Examples
Different annotation formats for the same annotation:
🔍 Runtime Metadata Access
BoxLang provides multiple ways to access annotation metadata at runtime, enabling powerful metaprogramming capabilities.
Using getMetadata()
The getMetadata() function returns metadata for any BoxLang object:
Using getClassMetadata()
Get metadata for a class without instantiating it:
Using $bx.meta
Every BoxLang object has a $bx.meta property that provides direct access to its metadata:
🎨 Common Annotation Patterns
Dependency Injection
Caching
Security & Authorization
Validation
Framework Integration
🔧 Custom Annotations
You can create your own annotations for custom framework features:
💻 Complete Example
🎯 Best Practices
Be Consistent - Use the same annotation style throughout your codebase
Document Custom Annotations - Create documentation for custom annotations your framework uses
Use Struct Literals for Complex Config - When annotations need multiple values, use struct literals for clarity
Leverage Metadata - Use runtime metadata inspection for framework features like DI, validation, and caching
Namespace Custom Annotations - Prefix custom annotations to avoid conflicts (
@myapp_feature)Keep It Simple - Don't overuse annotations; keep your code readable
Test Metadata Access - Ensure your metaprogramming code handles missing annotations gracefully
📚 Related Topics
Last updated
Was this helpful?
