架构

注册表

师成师成· 更新于 2026-09-28· 阅读 12 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

注册表(Registry)

org.apache.camel.spi.Registry API 是一个通用 API,用于在任何类型的运行时平台上查找 Bean,无论你是在 Spring Boot、Quarkus、独立运行、Camel CLI 还是其他环境中运行 Camel。

Camel 使用 DefaultRegistry,它会根据所使用的运行时平台,优先从该平台查找 Bean,若找不到则回退到 Camel 自带的 SimpleRegistry。

注册表 API

注册表包含两组 API:

  • 绑定(binding)
  • 查找(lookup)

绑定 API 用于向注册表中添加新的 Bean,查找 API 用于从注册表中查找已有的 Bean。

绑定 API

绑定 API 如下所示:

仅 Java:Registry 绑定 API

public interface Registry extends BeanRepository {

    /**
     * Binds the bean to the repository (if possible).
     * If the bean is CamelContextAware then the registry will automatically inject the context if possible.
     *
     * @param  id    the id of the bean
     * @param  bean  the bean
     */
    void bind(String id, Object bean);

    /**
     * Binds the bean to the repository (if possible).
     * Binding by id and type allows to bind multiple entries with the same id but with different type.
     * If the bean is CamelContextAware then the registry will automatically inject the context if possible.
     *
     * @param  id    the id of the bean
     * @param  type  the type of the bean to associate the binding
     * @param  bean  the bean
     */
    void bind(String id, Class<?> type, Object bean);

}
绑定 API 会将 Bean 绑定到 Camel 的内部注册表,而不是运行时(Spring Boot 等)。

例如,如果你需要将一个 Bean 添加到 Registry 中,可以直接在 Java 中这样操作:

仅 Java:将 Bean 绑定到 Registry

Object myFoo = ...
camelContext.getRegistry().bind("foo", myFoo);

然后你可以通过 id 访问该 bean,例如在 Camel 路由中:

仅 Java:在路由中使用 Registry 中的 bean

from("jms:cheese").bean("foo");

在 Spring XML 中绑定

如果使用 Spring XML 文件,那么任意 <bean> 都会自动由 Spring 自行处理,并注册到 Spring 的 bean 容器中;这意味着无需再由 Camel 绑定该 bean。

<bean id="foo" class="com.foo.MyFoo"/>

在 Spring Boot 中进行绑定

使用 Spring Boot 时,你也可以使用注解来声明 bean,例如在创建 bean 的方法上使用 @Bean 注解:

仅 Java:使用 Spring Boot 的 @Bean 注解声明 bean

@Bean
public MyFoo foo() {
    return new MyFoo();
}

这是 Spring Boot 的常见功能,你可以在 Spring Boot 项目文档中找到相关信息。

在 Quarkus 中绑定

Quarkus 拥有与 Spring Boot 类似的用于声明 Bean 的功能,可以使用 javax.inject.enterprise.Producer 和 javax.inject.Named 注解来完成:

仅 Java:使用 Quarkus CDI 注解声明一个 Bean

@Produces @Named("foo")
public MyFoo foo() {
    return new MyFoo();
}

查找 API

注册表(Registry)主要用于按 ID 或按类型查找 Bean。这在 Camel 启动过程中经常被使用,此时 Camel 正在装配所有的组件、端点、路由、处理器、Bean 等。

查找 API 包含以下方法:

仅限 Java:BeanRepository 查找 API

public interface BeanRepository {

    /**
     * Looks up a bean in the registry based purely on name, returning the bean or null if it could not be found.
     *
     * Important: Multiple beans of different types may be bound with the same name, and its encouraged to use the
     * lookupByNameAndType(String, Class) to lookup the bean with a specific type, or to use any of the
     * find methods.
     *
     * @param  name the name of the bean
     * @return      the bean from the registry or null if it could not be found
     */
    Object lookupByName(String name);

    /**
     * Looks up a bean in the registry, returning the bean or null if it could not be found.
     *
     * @param  name the name of the bean
     * @param  type the type of the required bean
     * @return      the bean from the registry or null if it could not be found
     */
    <T> T lookupByNameAndType(String name, Class<T> type);

    /**
     * Finds beans in the registry by their type.
     *
     * @param  type the type of the beans
     * @return      the types found, with their bean ids as the key. Returns an empty Map if none found.
     */
    <T> Map<String, T> findByTypeWithName(Class<T> type);

    /**
     * Finds beans in the registry by their type.
     *
     * @param  type the type of the beans
     * @return      the types found. Returns an empty Set if none found.
     */
    <T> Set<T> findByType(Class<T> type);

}

可以从 Java 代码中查找 Bean,如下所示:

纯 Java 方式:从 Registry 中查找 Bean

// lookup by id only
Object foo = camelContext.getRegistry().lookupByName("foo");

// lookup by type so there is no need for type casting
MyFoo foo2 = camelContext.getRegistry().lookupByNameAndType("foo", MyFoo.class);

查找 Bean

你也可以使用依赖注入,通过 Camel 注册表(registry)来查找 Bean。如果你使用的是 Spring Boot 或 Quarkus 等运行时平台,它们本身自带相应的功能。Camel 也有自己的 Bean 注入注解 @BeanInject,可在独立运行 Camel 时使用。

你也可以在 Spring Boot 或 Quarkus 中使用 Camel 的 @BeanInject;但这要求执行 Bean 注入的类由 Camel 管理(例如 RouteBuilder 类),而情况并不总是如此。因此,建议使用 Spring Boot 或 Quarkus 的注解。

在 Spring Boot 中查找

使用 Spring Boot 时,可以使用 @Autowired 或 @Inject 等 Spring 注解进行依赖注入。

在 Quarkus 中查找

使用 Quarkus 时,可以使用 @Inject 和 @Named 等 CDI 注解进行依赖注入。

更多信息

有关在 Camel 中使用 Bean 的更多细节,请参阅 Bean 注入 和 Bean 集成。

评论

登录后参与评论

正在加载评论…