Application.bx

Create virtual applications in memory with isolated settings, lifecycle events, and persistence scopes across all BoxLang runtimes

🌏 Overview

Application.bx is BoxLang's application framework - a powerful feature that allows you to define virtual applications in memory with isolated settings, lifecycle events, and persistence scopes. This works across all BoxLang runtimes: web servers (CommandBox, MiniServer), CLI applications, Lambda functions, desktop applications, and more.

What is Application.bx?

Application.bx is a special BoxLang class file that serves two primary purposes:

  1. Application Configuration - Define application-wide settings in the pseudo-constructor using the this scope:

    • Application name and timeouts

    • Datasource configurations

    • Caching strategies

    • Session management

    • File mappings and class paths

    • Java library integration

    • Security settings

    • Custom schedulers and much more

  2. Lifecycle Event Handlers - Implement callback methods that BoxLang executes automatically at key points:

    • Application startup/shutdown

    • Session creation/destruction

    • Request processing (start, execute, end)

    • Error handling

    • Missing templates

    • Class invocations

How It Works

When BoxLang executes any code (web request, CLI script, Lambda function), it searches for Application.bx starting from the current directory and traversing upward through parent directories until found or reaching the root.

πŸ“‹ Table of Contents

Directory Scope: The Application.bx file applies to its directory and all subdirectories. Any BoxLang code in that tree will automatically use this application context.

Transient Nature

Application.bx is instantiated on every request - this is a critical feature that provides:

βœ… Dynamic Configuration - Modify settings per-request based on conditions:

  • Switch datasources based on subdomain or user

  • Adjust session timeouts for bot detection

  • Enable/disable features based on environment

  • Dynamic security rules

βœ… Request-Level Customization - Each request can have unique behavior while sharing the same application memory space

⚠️ Performance Consideration - Since it runs on every request, keep the pseudo-constructor logic optimized. Use the application scope for expensive operations that should only run once.

Multi-Runtime Support

Application.bx works seamlessly across all BoxLang deployment targets:

Runtime
Use Case
Application.bx Behavior

Web Servers

CommandBox, MiniServer, JEE

Full support with sessions, cookies, web scopes

CLI

Scripts, automation, tools

Application scope, no web-specific features

Lambda

Serverless functions

Application scope, cold start optimization

Desktop

Electron, JavaFX apps

Application scope, local persistence

πŸ“ Complete Example

βš™οΈ Configuration Settings

All configuration settings are defined in the pseudo-constructor using the this scope. Here's a comprehensive reference of available settings:

Core Application Settings

Setting
Type
Default
Description

this.name

string

Generated

Unique application name. Defines the memory space reservation

this.applicationTimeout

timespan

0,0,0,0

Application lifetime. Default 0,0,0,0 = never expires (recommended)

this.locale

string

JVM locale

Default locale (e.g., "en_US", "es-ES")

this.timezone

string

JVM timezone

IANA timezone (e.g., "UTC", "America/New_York")

Session Management (Web Runtime)

Setting
Type
Default
Description

this.sessionManagement

boolean

false

Enable session tracking

this.sessionTimeout

timespan

0,0,30,0

Session lifetime (30 minutes default)

this.sessionStorage

string

"memory"

Cache name for session storage or "memory"

this.setClientCookies

boolean

true

Automatically set session cookies

this.setDomainCookies

boolean

false

Share cookies across subdomains

Datasources

Setting
Type
Description

this.datasource

string

Default datasource name

this.defaultDatasource

string

Alias for this.datasource

this.datasources

struct

Datasource definitions

Example:

See datasource configuration for full configuration details.

Caching

Define application-specific caches that BoxLang manages automatically:

See Caching documentation for full configuration details.

Mappings

Define virtual paths for class and file resolution:

As of BoxLang 1.6.0, mappings support both simple (string) and complex (struct) formats:

Java Integration

Load Java libraries and manage class loading:

See Java Integration documentation for details.

Custom Schedulers

Register scheduler classes that run automatically:

See Asynchronous Programming documentation for scheduler details.

Security Settings

Setting
Type
Default
Description

this.invokeImplicitAccessor

boolean

Context

Enable implicit getters/setters

this.allowedFileOperationExtensions

array

Runtime

File extensions allowed for file operations

this.disallowedFileOperationExtensions

array

Runtime

File extensions disallowed for file operations

See security configuration for full details.

Advanced Settings

Setting
Type
Description

this.classPaths

array

Global class paths for .bx files

this.componentPaths

array

Alias for classPaths

this.customComponentPaths

array

Custom component directories

πŸ”„ Lifecycle Events

Application.bx acts as a comprehensive event listener, with BoxLang automatically invoking callback methods at key moments in your application's lifecycle.

Application Lifecycle

onApplicationStart()

Executed once when the application first starts - when the first request arrives and the application doesn't exist in memory.

When it runs:

  • First request after server startup

  • After application timeout expires

  • After applicationStop() is called

onApplicationEnd( struct applicationScope )

Executed once when the application shuts down due to timeout or explicit stop.

Session Lifecycle (Web Runtime Only)

onSessionStart()

Executed when a new user session begins.

onSessionEnd( struct sessionScope, struct applicationScope )

Executed when a session expires or is explicitly terminated.

Request Lifecycle

onRequestStart( string targetPage )

Executed at the start of every request, before the target page is processed.

Return Value:

  • true - Continue processing the request

  • false - Abort the request (no further processing)

onRequest( string targetPage )

Wraps the entire request execution. You control if/how the target page is included.

Pattern: Think of onRequestStart() as "before advice" and onRequest() as "around advice" in AOP terms. If you implement onRequest(), you must include the target page yourself.

onRequestEnd()

Executed after the request completes, even if errors occurred.

Error Handling

onError( any exception, string eventName )

Global error handler - catches any unhandled exceptions in your application.

Parameters:

  • exception - The exception struct with message, detail, type, stacktrace, etc.

  • eventName - Which lifecycle event threw the error (e.g., "onRequestStart", "onApplicationStart")

onAbort( required string targetPage )

Executed when abort() is called anywhere in the request.

Special Handlers

onMissingTemplate( required string targetPage )

Executed when a requested template doesn't exist - your custom 404 handler.

onClassRequest( className, method, struct args )

Intercepts remote class invocations (HTTP/AMF calls to BoxLang classes).

Execution Order

πŸ—οΈ Virtual Applications - A Critical Feature

One of BoxLang's most powerful capabilities is the ability to create multiple virtual applications within a single JVM process. Each application is a memory space reservation with isolated scopes and settings.

How Virtual Applications Work

Each Application.bx with a unique this.name creates a separate virtual application. These applications:

βœ… Have their own isolated application scope and timeout

βœ… Have their own isolated session scopes (web runtime), caches and timeouts

βœ… Can have completely different settings and configurations

βœ… Share the same JVM but are logically independent

βœ… Can be nested or side-by-side in the directory structure

Example: Multiple Apps in One Server

Result: Three independent applications running in the same JVM:

  • PublicSite - Public website with 30-day application timeout

  • AdminConsole - Admin area with 1-hour session timeout and different datasource

  • RestAPI - API endpoints with no session management

Practical Example

Scope Isolation

Application Longevity

Applications live in memory for the duration specified by this.applicationTimeout:

When an application expires:

  1. onApplicationEnd() is called

  2. Application scope is destroyed

  3. Next request triggers onApplicationStart() and creates a new application instance

Manual Control:

Why you can't "kill" the application scope: Applications are time-based memory reservations. They expire automatically based on applicationTimeout or when explicitly stopped with applicationStop().

Use Cases for Virtual Applications

🎯 Multi-Tenant SaaS

🎯 Microservices Architecture

🎯 Environment Separation

🎯 Legacy Migration

πŸ“š Additional Resources

CFML Compatibility: For CFML compatibility reference, see CFDocs Application.cfc. BoxLang supports the majority of CFML Application.cfc features with enhanced capabilities.

Last updated

Was this helpful?