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.
The Big Picture: Virtual Applications
BoxLang creates isolated virtual applications within a single JVM process:
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ BoxLang Runtime (JVM) ┃
┃ ┃
┃ ┌──────────────────────-┐ ┌──────────────────────┐ ┃
┃ │ Application: "App1" │ │ Application: "App2" │ ┃
┃ │ ─────────────────── │ │ ─────────────────── │ ┃
┃ │ 📦 application{} │ │ 📦 application{} │ ┃
┃ │ 👤 session{} │ │ 👤 session{} │ ┃
┃ │ ⚙️ Config Settings │ │ ⚙️ Config Settings │ ┃
┃ │ 🗄️ Datasources │ │ 🗄️ Datasources │ ┃
┃ │ 🧩 Lifecycle Events │ │ 🧩 Lifecycle Events │ ┃
┃ └───────────────────────┘ └──────────────────────┘ ┃
┃ ↑ ↑ ↑ ↑ ↑ ↑ ┃
┃ Requests from Requests from ┃
┃ /app1/** tree /app2/** tree ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛Each Application.bx creates a completely isolated virtual application with its own memory space, configuration, and lifecycle - all running in the same JVM.
Key Feature: With a single Application.bx file or a hierarchy of them, you can create virtual applications in memory, each with its own isolated application, session, and configuration space - all within the same JVM process.
What is Application.bx?
Application.bx is a special BoxLang class file that serves two primary purposes:
Application Configuration - Define application-wide settings in the pseudo-constructor using the
thisscope: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
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.
Application.bx Discovery Process
Nested Applications Example
Transient Nature
Application.bx is instantiated on every request - this is a critical feature that provides multi-tenancy for any BoxLang web application out of the box:
✅ 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.
Application.bx Instantiation vs Application Memory
The Key Insight:
🔄 Application.bx class = Created and destroyed with every request
💾
applicationscope = Persists in memory and shared across all requests
Multi-Runtime Support
Application.bx works seamlessly across all BoxLang deployment targets:
Web Servers
CommandBox, MiniServer, JEE
Full support with sessions, cookies, web scopes
CLI
Scripts, automation, tools
Application scope, no web-specific features
AWS Lambda
Serverless functions
Application scope, cold start optimization
Google Cloud Functions
Serverless functions
Application scope, cold start optimization
Azure Functions
Serverless functions
Application scope, cold start optimization
Desktop
Electron, JavaFX apps
Application scope, local persistence
Web-Only Scopes: Features like session, cookie, form, url, and cgi scopes only exist in web runtimes. Structure your code to handle different runtime contexts gracefully.
📝 Complete Example
📚 Additional Resources
Configuration: See Configuration documentation for runtime-level settings
Caching: See Caching documentation for cache strategies
Async: See Asynchronous Programming for schedulers and executors
Datasources: See Datasource configuration
Java Integration: See Java Integration for loading Java libraries
Last updated
Was this helpful?
