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

Using beans in your Camel integrations provides several key advantages:

  1. Reusability — Define once, reference multiple times across different routes without duplicating configuration
  2. Centralized configuration — Manage connection details, credentials, and settings in one place for easier maintenance
  3. Type safety — Leverage Java’s type system with fully qualified class names for compile-time validation
  4. Separation of concerns — Keep infrastructure configuration separate from business logic in your routes
  5. Testability — Mock or replace beans easily during testing without modifying route definitions
  6. Performance — Share expensive resources like connection pools across multiple routes efficiently

7.1.1. Common use cases

  1. Data sources — database connection pools (e.g., PGSimpleDataSource, HikariDataSource)
  2. HTTP clients — pre-configured REST or HTTP clients with base URLs and authentication
  3. Custom processors — Java classes implementing Camel’s Processor interface for custom logic
  4. Caches — in-memory or distributed cache instances (e.g., Caffeine, Redis)
  5. Serializers / Marshallers — Jackson ObjectMapper or JAXB context beans
  6. 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

Beans can also execute custom business logic in your routes:

  1. 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.
  2. 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.

Note

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.

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

  1. Open a Camel integration file in Kaoto (or create a new one named book-database.camel.yaml).
  2. Click the Beans tab at the top of the Kaoto editor to open the Beans Editor.

    01 beans tab
  3. Click Add new Beans at the top of the bean list.

    02 add beans button
  4. In the Details form at the right, fill bean name and Java type:

    • name bookDataSource
    • type org.postgresql.ds.PGSimpleDataSource

      03 bean name type
  5. Lower in the Details form, add the connection details under properties. Click Add a new property button:

    04 bean properties
  6. Fill properties name and value as following:

    KeyValue

    serverName

    localhost

    databaseName

    bookstore

    user

    postgres

    password

    {{db.password}}

    portNumber

    5432

    04 02 bean properties added
  7. This adds beans block

    - 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

Create a route that uses this datasource bean to insert book records:

  1. Click the Design tab to return to the canvas
  2. Kaoto creates a default timer-to-log route. Configure the Timer component:

    • Timer Name bookInsertTimer
    • Period 10000 (runs every 10 seconds)
  3. 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 #bookDataSource from the dropdown
  4. Update the Log step message:

    • Message Book inserted successfully: ${body}
  5. The generated structure looks like this:

    09 route

7.2.3. Running the integration

  1. Ensure PostgreSQL is running and the bookstore database exists
  2. Click the Run button in the toolbar of the Kaoto editor to start the integration.

    10 run button
  3. The integration will start and begin inserting book records every 10 seconds.

    11 book insert

7.2.4. Expected Output

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

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:

idtitleauthorisbnpublished_yearcreated_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

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}"
Note

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.

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

  1. Open a Camel integration file in Kaoto and click the Design tab at the top of the Kaoto editor.
  2. Kaoto creates a default timer-to-log route. Insert an SQL component between Timer and Log.
  3. 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.
  4. Locate the Data Source field and click it.

    12 data source
  1. In the dropdown, click Create new bean image::13-create-new-bean-option.png[]
  2. A modal dialog opens — fill in:

    • name bookstoreDataSource
    • type org.postgresql.ds.PGSimpleDataSource

      14 create new bean modal
  3. Add the connection details under properties:

    KeyValue

    serverName

    localhost

    databaseName

    bookstore

    user

    postgres

    password

    {{db.password}}

    portNumber

    5432

    15 create new bean properties
  4. Click Create

    16 create new bean create
  5. The bean is added to the beans list and the Data Source field is automatically set to reference it — in a single action.

    17 datasource set
  6. This adds beans block
- 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

  1. Click the Beans tab at the top of the Kaoto editor.

    01 beans tab
  2. 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.

    18 bean details
  3. Modify the values as needed; changes are applied immediately.

    19 bean properties
  4. 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.

    20 bean properties add
    21 bean properties remove

7.5. Deleting a Bean

  1. Click the Beans tab at the top of the Kaoto editor.
  2. Click the Delete button (trash icon) on the right side of the bean row.

    22 delete bean
Warning

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.

Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat Documentation

Legal Notice

Theme

© 2026 Red Hat
Back to top