GrainContext Annotations

Java only

These annotations can assist you in writing and maintaining your handlers more effectively.

1. @GrainiteLogger

This annotation is used to inject the logger into an annotated field. This is equivalent to getting the logger using GrainContext#getLogger().

The field type for this annotation must be java.util.logging.Logger.

@GrainiteLogger example
public class MyHandlersClass {
    @GrainiteLogger Logger logger;
    
    public ActionResult handleAction(Action action, GrainContext context) {
        logger.info("Hello using annotated logger!");
        return ActionResult.success(action);
    }
}

2. @InjectGrainContext

This annotation is used to inject the GrainContext into the annotated field. With this, you don't need to include the GrainContext parameter in your handler methods.

The field type for this annotation must be com.grainite.api.context.GrainContext.

@InjectGrainContext example
public class MyHandlersClass {
    @InjectGrainContext GrainContext context;
    
    public ActionResult handleAction(Action action) {
        Value val = context.getValue();
        return ActionResult.success(action);
    }
}

3. @ActionHandlerConfig

This annotation is used to inject the Action Handler's config into the annotated field. This is equivalent to getting the config using GrainContext#getConfig().

The field type for this annotation must be java.util.Map.

@ActionHandlerConfig example
public class MyHandlersClass {
    @ActionHandlerConfig Map<String, String> config;
    
    public ActionResult handleAction(Action action, GrainContext context) {
        String dbURL = config.get("db_url");
        return ActionResult.success(action);
    }
}

Last updated