cancel
Showing results for 
Search instead for 
Did you mean: 

Exposing configuration beans with Spring Boot

richardmward
Champ in-the-making
Champ in-the-making
Hi,

I have set up an application using the spring boot starters - which was a wonderfully simple process - thanks! I am now in need of adding some customisation to the basic configuration and am wondering if anyone can provide any advise what the 'correct' approach to doing this is.

The two pieces of configuration I most need is to define the beans that are exposed to the activiti processes (http://www.activiti.org/userguide/#exposingConfigurationBeans) and configuring custom form types (http://www.jorambarrez.be/blog/2013/03/13/creating-a-new-form-property-in-activiti/).

Any advise gratefully received in advance!
3 REPLIES 3

richardmward
Champ in-the-making
Champ in-the-making
*Update* I have gone with the following implmentation based on a stack overflow reply (http://stackoverflow.com/a/28041720). I'd be interested to know if there is either a better approach, or if there are any pitfalls with this approach. The only thing I notice is that my custom form and service delegate beans are now created sooner than they previously were. Whether that is an issue, I'm not yet sure.

<code>
    @Bean
    public BeanPostProcessor activitiConfigurer() {
        return new BeanPostProcessor() {

            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof SpringProcessEngineConfiguration) {
                    // If it is the SpringProcessEngineConfiguration, we want to add some extra configuration.
                    SpringProcessEngineConfiguration config = (SpringProcessEngineConfiguration) bean;
                    // Add our custom form types.
                    config.setCustomFormTypes(getCustomFormTypes());
                    // Limit the beans that are available to process definitions.
                    config.setBeans(getServiceDelegates());
                }
                return bean;
            }

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                return bean;
            }

            /**
             * Find all the custom form types that we have added. All our form types extend an abstract class that provides some extra functionality, so we
             * can find the beans based on that abstract class.
             *
             * @return a list of custom form types.
             */
            private List<AbstractFormType> getCustomFormTypes() {
                Map<String, AbstractIHFormType> beans = applicationContext.getBeansOfType(MyAbstractFormType.class);
                List<AbstractFormType> abstractFormTypes = new ArrayList<>();
                if (beans != null) {
                    abstractFormTypes.addAll(beans.values());
                }
                return abstractFormTypes;
            }

            /**
             * Find all the services delegates we are exposing to the process definitions. We have added a custom annotation which we can look for to find these
             * beans with.
             *
             * @return a list of the available service delegates.
             */
            private Map<Object, Object> getServiceDelegates() {
                Map<String, Object> beans = applicationContext.getBeansWithAnnotation(MyServiceDelegate.class);
                Map<Object, Object> serviceDelegates = new HashMap<>();
                if (beans != null) {
                    serviceDelegates.putAll(beans);
                }
                return serviceDelegates;
            }
        };
    }
</code>

jbarrez
Star Contributor
Star Contributor
You can simply have @Bean's for your service tasks.

The form types solution is a nice one, didn't think about that. Alternatively, you can override the spring boot auto creation of the process egnine bean … but this also works nicely and you dont have to copy paste the default code.

rajagopalan
Champ in-the-making
Champ in-the-making
Hi,

I also have a very similar requirement as you have explained in this case. I am trying to add a custom VariableType to the Process Engine Configuration. I use spring boot activiti v5.19.0. My process definitions heavily use Activiti Camel Endpoints. So that is also included in my maven dependency. Spring Boot initializes the process engine with all the default configuration.

I have created a BeanPostProcessor as you have mentioned to get hold of SpringProcessEngineConfiguration, which should be the bean created to initialize the Process Engine. Even though my postProcessBeforeInitialization() method is called for all beans, it is never called for SpringProcessEngineConfiguration bean. Hence I am unable to add a PreCustomVariableType to the engine configuration.

Any info on what could be the reason why the BeanPostProcessor is not called for SpringProcessEngineConfiguration (or) any alternate way for adding a PreCustomVariableType to engine configuration would be really helpful.