Chapter 7. Beans
Beans allows you to define custom Java object instances that can be referenced and reused across your Apache Camel routes — such as a database connection pool, a custom processor, or a third-party client. If you completed the Quickstart, you can extend that integration by adding beans to it.
A bean has a name (its identifier within the integration), a type (the fully qualified Java class name), and optional configuration such as properties, constructor arguments, or a factory method. Once defined, a bean can be referenced from any step in your routes.
7.1. Overview of Beans in Kaoto Copy linkLink copied to clipboard!
Using beans in your Camel integrations provides several key advantages:
- Reusability — Define once, reference multiple times across different routes without duplicating configuration
- Centralized configuration — Manage connection details, credentials, and settings in one place for easier maintenance
- Type safety — Leverage Java’s type system with fully qualified class names for compile-time validation
- Separation of concerns — Keep infrastructure configuration separate from business logic in your routes
- Testability — Mock or replace beans easily during testing without modifying route definitions
- Performance — Share expensive resources like connection pools across multiple routes efficiently
7.1.1. Common use cases Copy linkLink copied to clipboard!
-
Data sources — database connection pools (e.g.,
PGSimpleDataSource,HikariDataSource) -
HTTP clients — pre-configured
RESTorHTTPclients with base URLs and authentication -
Custom processors — Java classes implementing Camel’s
Processorinterface for custom logic -
Caches — in-memory or distributed cache instances (e.g.,
Caffeine,Redis) -
Serializers / Marshallers — Jackson
ObjectMapperorJAXBcontext beans - Business logic beans — Custom Java classes containing business logic invoked via the Bean component or Bean EIP
7.1.2. Bean Component and Bean EIP Copy linkLink copied to clipboard!
Beans can also execute custom business logic in your routes:
- Bean EIP — The standard way to invoke bean methods as processing steps within a route. Use this for most scenarios where you need to execute business logic mid-route.
-
Bean Component — Invokes beans using URI syntax (
uri: bean:beanName?method=methodName). Use this when you need to dynamically construct the URI from runtime values such as headers or variables.
Both allow you to define business logic in Java classes and reference them as beans, keeping routes clean and logic reusable.
Kaoto offers two approaches for working with beans, covered in the scenarios below.
Configuration Best Practice: The examples in this guide use Camel’s property placeholder syntax like {{db.password}} for sensitive values. These properties are resolved at runtime from an application.properties file in your project. For example:
db.password=your_actual_password
For production deployments, use secret managers (e.g., Kubernetes Secrets) or platform-specific secret injection mechanisms.
7.2. Scenario 1: Defining a bean first, then referencing it in a route step Copy linkLink copied to clipboard!
This scenario demonstrates creating a PostgreSQL datasource bean for a book database, connecting it to a route, and running the complete integration.
Prerequisites
A running PostgreSQL instance with a database named bookstore and database credentials ready.
CREATE DATABASE bookstore;
\c bookstore
CREATE TABLE books (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
isbn VARCHAR(13),
published_year INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
7.2.1. Creating the bean in the Beans Editor Copy linkLink copied to clipboard!
-
Open a Camel integration file in Kaoto (or create a new one named
book-database.camel.yaml). Click the Beans tab at the top of the Kaoto editor to open the Beans Editor.
Click Add new Beans at the top of the bean list.
In the Details form at the right, fill bean name and Java type:
-
name
bookDataSource type
org.postgresql.ds.PGSimpleDataSource
-
name
Lower in the Details form, add the connection details under properties. Click Add a new property button:
Fill properties name and value as following:
Key Value serverNamelocalhostdatabaseNamebookstoreuserpostgrespassword{{db.password}}portNumber5432
This adds
beansblock- beans: - name: bookDataSource type: org.postgresql.ds.PGSimpleDataSource properties: serverName: localhost databaseName: bookstore user: postgres password: '{{db.password}}' portNumber: 5432
7.2.2. Creating a route using the bean Copy linkLink copied to clipboard!
Create a route that uses this datasource bean to insert book records:
- Click the Design tab to return to the canvas
Kaoto creates a default timer-to-log route. Configure the Timer component:
-
Timer Name
bookInsertTimer -
Period
10000(runs every 10 seconds)
-
Timer Name
Insert an SQL component between Timer and Log:
-
Query
INSERT INTO books (title, author, isbn, published_year) VALUES ('Camel Integration Patterns', 'John Smith', '978-0321200686', 2003) -
Data Source
Select #bookDataSourcefrom the dropdown
-
Query
Update the Log step message:
-
Message
Book inserted successfully: ${body}
-
Message
The generated structure looks like this:
7.2.3. Running the integration Copy linkLink copied to clipboard!
-
Ensure PostgreSQL is running and the
bookstoredatabase exists Click the Run button in the toolbar of the Kaoto editor to start the integration.
The integration will start and begin inserting book records every 10 seconds.
7.2.4. Expected Output Copy linkLink copied to clipboard!
When the integration runs successfully, you should see output similar to:
INFO [main] Apache Camel 4.18.1 (book-database) started in 156ms
INFO [bookInsertTimer] Book inserted successfully: 1
INFO [bookInsertTimer] Book inserted successfully: 1
7.2.5. Verifying the Data Copy linkLink copied to clipboard!
Connect to your PostgreSQL database and query the books table:
SELECT * FROM books ORDER BY created_at DESC LIMIT 5;
You should see the newly inserted book records:
| id | title | author | isbn | published_year | created_at |
|---|---|---|---|---|---|
| 2 | Camel Integration Patterns | John Smith | 978-0321200686 | 2003 | 2026-04-03 14:59:57.165154 |
| 1 | Camel Integration Patterns | John Smith | 978-0321200686 | 2003 | 2026-04-03 14:59:47.221565 |
7.2.6. Complete YAML Integration Copy linkLink copied to clipboard!
Here’s the complete working integration combining the bean definition and route:
- beans:
- name: bookDataSource
type: org.postgresql.ds.PGSimpleDataSource
properties:
serverName: localhost
databaseName: bookstore
user: postgres
password: '{{db.password}}'
portNumber: 5432
- route:
id: route-book-insert
from:
uri: timer
parameters:
timerName: bookInsertTimer
period: "10000"
steps:
- to:
uri: sql
parameters:
query: INSERT INTO books (title, author, isbn, published_year) VALUES ('Camel Integration Patterns', 'John Smith', '978-0321200686', 2003)
dataSource: "#bookDataSource"
- log:
message: "Book inserted successfully: ${body}"
This bean-based database infrastructure can later be referenced by REST endpoints for a complete book management API (e.g., GET /books, POST /books, GET /books/{id}). The datasource bean provides a reusable, centrally configured database connection that multiple routes and REST endpoints can share.
7.3. Scenario 2: Starting from the step, creating the bean inline Copy linkLink copied to clipboard!
This scenario demonstrates creating a bean directly from a component’s configuration panel, useful while building a route and when you need a new bean. Use the same book insertion example, but create the bean inline while configuring the SQL component.
7.3.1. Adding an SQL step to your route Copy linkLink copied to clipboard!
- Open a Camel integration file in Kaoto and click the Design tab at the top of the Kaoto editor.
- Kaoto creates a default timer-to-log route. Insert an SQL component between Timer and Log.
In the SQL step configuration, you’ll need to specify:
-
Query —
INSERT INTO books (title, author, isbn, published_year) VALUES ('Camel Integration Patterns', 'John Smith', '978-0321200686', 2003). - Data Source — Reference to a datasource bean.
-
Query —
Locate the Data Source field and click it.
7.3.2. Creating the bean from the Data Source field Copy linkLink copied to clipboard!
- In the dropdown, click Create new bean image::13-create-new-bean-option.png[]
A modal dialog opens — fill in:
-
name
bookstoreDataSource type
org.postgresql.ds.PGSimpleDataSource
-
name
Add the connection details under properties:
Key Value serverNamelocalhostdatabaseNamebookstoreuserpostgrespassword{{db.password}}portNumber5432
Click Create
The bean is added to the beans list and the Data Source field is automatically set to reference it — in a single action.
-
This adds
beansblock
- beans:
- name: bookstoreDataSource
type: org.postgresql.ds.PGSimpleDataSource
properties:
serverName: localhost
databaseName: bookstore
user: postgres
password: '{{db.password}}'
portNumber: 5432
7.4. Editing a Bean Copy linkLink copied to clipboard!
Click the Beans tab at the top of the Kaoto editor.
In the Beans list at the left, click the row for the bean you want to edit — it shows all fields of the Bean at right.
Modify the values as needed; changes are applied immediately.
To add a property, fill in a new key-value pair in the properties section. To remove a property, click the remove icon next to it.
7.5. Deleting a Bean Copy linkLink copied to clipboard!
- Click the Beans tab at the top of the Kaoto editor.
Click the Delete button (trash icon) on the right side of the bean row.
Deleting a bean that is still referenced by a route step leaves a dangling reference. Update or remove the affected step configurations before deleting the bean.