public static CamelContext doConfigureCamelContext()

in core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java [124:232]


    public static CamelContext doConfigureCamelContext(ApplicationContext applicationContext, CamelContext camelContext,
                                                       CamelConfigurationProperties config,
                                                       CamelSpringBootApplicationController controller) throws Exception {

        // inject camel context on controller
        CamelContextAware.trySetCamelContext(controller, camelContext);

        // setup startup recorder before building context
        configureStartupRecorder(camelContext, config);

        camelContext.build();

        var listeners = controller.getMain().getMainListeners();
        if (!listeners.isEmpty()) {
            for (MainListener listener : listeners) {
                listener.beforeInitialize(controller.getMain());
            }
            // allow doing custom configuration before camel is started
            for (MainListener listener : listeners) {
                listener.beforeConfigure(controller.getMain());
            }
        }

        // initialize properties component eager
        PropertiesComponent pc = applicationContext.getBeanProvider(PropertiesComponent.class).getIfAvailable();
        if (pc != null) {
            pc.setCamelContext(camelContext);
            camelContext.setPropertiesComponent(pc);
        }

        final Map<String, BeanRepository> repositories = applicationContext.getBeansOfType(BeanRepository.class);
        if (!repositories.isEmpty()) {
            List<BeanRepository> reps = new ArrayList<>();
            // include default bean repository as well
            reps.add(new ApplicationContextBeanRepository(applicationContext));
            // and then any custom
            reps.addAll(repositories.values());
            // sort by ordered
            OrderComparator.sort(reps);
            // and plugin as new registry
            camelContext.getCamelContextExtension().setRegistry(new DefaultRegistry(reps));
        }

        if (ObjectHelper.isNotEmpty(config.getMain().getFileConfigurations())) {
            Environment env = applicationContext.getEnvironment();
            if (env instanceof ConfigurableEnvironment) {
                MutablePropertySources sources = ((ConfigurableEnvironment) env).getPropertySources();
                if (!sources.contains("camel-file-configuration")) {
                    sources.addFirst(new FilePropertySource("camel-file-configuration", applicationContext,
                            config.getMain().getFileConfigurations()));
                }
            }
        }

        // configure camel.variable.xx configurations
        Environment env = applicationContext.getEnvironment();
        if (env instanceof ConfigurableEnvironment cev) {
            Map<String, String> vars = doExtractVariablesFromSpringBoot(cev);
            if (!vars.isEmpty()) {
                // set variables
                for (String key : vars.keySet()) {
                    String value = vars.get(key);
                    String id = StringHelper.before(key, ":", "global");
                    key = StringHelper.after(key, ":", key);
                    VariableRepository repo = camelContext.getCamelContextExtension()
                            .getContextPlugin(VariableRepositoryFactory.class).getVariableRepository(id);
                    // it may be a resource to load from disk then
                    if (value.startsWith(LanguageSupport.RESOURCE)) {
                        value = value.substring(9);
                        if (ResourceHelper.hasScheme(value)) {
                            InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, value);
                            value = IOHelper.loadText(is);
                            IOHelper.close(is);
                        }
                    }
                    repo.setVariable(key, value);
                }
            }
        }

        // setup cli connector eager
        configureCliConnector(applicationContext, camelContext);

        camelContext.getCamelContextExtension().addContextPlugin(PackageScanClassResolver.class,
                new FatJarPackageScanClassResolver());
        camelContext.getCamelContextExtension().addContextPlugin(PackageScanResourceResolver.class,
                new FatJarPackageScanResourceResolver());

        if (config.getMain().getRouteFilterIncludePattern() != null
            || config.getMain().getRouteFilterExcludePattern() != null) {
            LOG.info("Route filtering pattern: include={}, exclude={}", config.getMain().getRouteFilterIncludePattern(),
                    config.getMain().getRouteFilterExcludePattern());
            camelContext.getCamelContextExtension().getContextPlugin(Model.class).setRouteFilterPattern(
                    config.getMain().getRouteFilterIncludePattern(), config.getMain().getRouteFilterExcludePattern());
        }

        // configure the common/default options
        DefaultConfigurationConfigurer.configure(camelContext, config.getMain());
        // lookup and configure SPI beans
        DefaultConfigurationConfigurer.afterConfigure(camelContext);
        // and call after all properties are set
        DefaultConfigurationConfigurer.afterPropertiesSet(camelContext);

        for (MainListener listener : listeners) {
            listener.afterConfigure(controller.getMain());
        }

        return camelContext;
    }