/** * Applies the {@link DefaultCamelBeanPostProcessor} to this instance. * * Derived classes using IoC / DI frameworks may wish to turn this into a NoOp such as for CDI * we would just use CDI to inject this */ protected void applyCamelPostProcessor() throws Exception { // use the default bean post processor from camel-core if the test class is not dependency injected already by Spring boolean spring = hasClassAnnotation("org.springframework.boot.test.context.SpringBootTest", "org.springframework.context.annotation.ComponentScan"); if (!spring) { DefaultCamelBeanPostProcessor processor = new DefaultCamelBeanPostProcessor(context); processor.postProcessBeforeInitialization(this, getClass().getName()); processor.postProcessAfterInitialization(this, getClass().getName()); } }
private Object registerResBean(Class clazz, String beanName, Plugin plugin, ClassLoader classLoader, CamelContext camelContext) throws Exception { //NOSONAR ClassLoader contextLoader = currentThread().getContextClassLoader(); currentThread().setContextClassLoader(classLoader); try { if (!(beanFactory instanceof ConfigurableListableBeanFactory) || !(beanFactory instanceof BeanDefinitionRegistry)) { throw new PluginsSystemException("Failed to initialize the resources beans: beanFactory must be " + "instance of BeanDefinitionRegistry and ConfigurableListableBeanFactory"); } final ConfigurableListableBeanFactory defaultBeanFactory = (ConfigurableListableBeanFactory) beanFactory; final BeanDefinitionRegistry beanReg = (BeanDefinitionRegistry) beanFactory; // replace old definitions with the new ones if (defaultBeanFactory.containsBeanDefinition(beanName)) { ((BeanDefinitionRegistry) defaultBeanFactory).removeBeanDefinition(beanName); } beanReg.registerBeanDefinition(beanName, new ClassLoaderBeanDefinition(clazz, classLoader)); final Object res = defaultBeanFactory.createBean(clazz, AUTOWIRE_BY_NAME, true); defaultBeanFactory.autowireBean(res); DefaultCamelBeanPostProcessor processor = new DefaultCamelBeanPostProcessor(camelContext); processor.postProcessBeforeInitialization(res, null); plugin.getContext().getInjector().inject(res, plugin.getContext(), null); defaultBeanFactory.registerSingleton(beanName, res); return res; } finally { currentThread().setContextClassLoader(contextLoader); } }
/** * Process the injection of the fields from the both Camel & Spring contexts */ public static void autowireFields(Object bean, ApplicationContext applicationContext, CamelContext camelContext) throws Exception { //NOSONAR if (applicationContext != null) { beanPostProcessor.setBeanFactory(applicationContext.getAutowireCapableBeanFactory()); beanPostProcessor.processInjection(bean); if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(applicationContext); } } if (camelContext != null) { DefaultCamelBeanPostProcessor processor = new DefaultCamelBeanPostProcessor(camelContext); processor.postProcessBeforeInitialization(bean, null); if (bean instanceof CamelContextAware) { ((CamelContextAware) bean).setCamelContext(camelContext); } } } }