cancel
Showing results for 
Search instead for 
Did you mean: 

Spring @Configuration with Activiti

kebo
Champ in-the-making
Champ in-the-making
Hi,
I'm new in activiti and I met problem with configuration activiti. I get null pointer when I want to load this configuration:


package org.workflow.config;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
public class ActivitiConfiguration {

   @Bean
   public BasicDataSource dataSource(){
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName("org.postgresql.Driver");
      dataSource.setUrl("jdbc:postgresql://localhost:5432/workflow");
      dataSource.setUsername("****");
      dataSource.setPassword("****");
      dataSource.setDefaultAutoCommit(false);
      
      return dataSource;
   }
   
   @Bean
   public DataSourceTransactionManager dataSourceTransactionManager(){
      DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
      dataSourceTransactionManager.setDataSource(dataSource());
      
      return dataSourceTransactionManager;
   }
   
   @Bean
   public SpringProcessEngineConfiguration processEngineConfiguration(){
      SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
      configuration.setDataSource(dataSource());
      configuration.setDatabaseType("postgresql");
      configuration.setTransactionManager(dataSourceTransactionManager());
      configuration.setDatabaseSchemaUpdate("true");
      configuration.setJobExecutorActivate(true);
      
      return configuration;
   }
   
   @Bean
   public ProcessEngineFactoryBean processEngineFactoryBean(){
      ProcessEngineFactoryBean bean = new ProcessEngineFactoryBean();
      bean.setProcessEngineConfiguration(processEngineConfiguration());
      
      return bean;
   }
   
   
   @Bean RuntimeService runtimeService(){
      return processEngineFactoryBean().getProcessEngineConfiguration().getRuntimeService();
   }
   
   @Bean
   public IdentityService identityService(){
      return processEngineFactoryBean().getProcessEngineConfiguration().getIdentityService();
   }
}


I build this configuration according to xml configuation. If you have any ideas please help me Smiley Happy

Best regards
3 REPLIES 3

patelag
Champ in-the-making
Champ in-the-making
Please share complete error stack trace.
I think you have missed injection of of activiti object

kebo
Champ in-the-making
Champ in-the-making
<code>
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'runtimeService' defined in class org.workflow.config.ActivitiConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [org.activiti.engine.RuntimeService org.workflow.config.ActivitiConfiguration.runtimeService()] threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1015)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.workflow.config.App.main(App.java:12)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [org.activiti.engine.RuntimeService org.workflow.config.ActivitiConfiguration.runtimeService()] threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:169)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570)
… 12 more
Caused by: java.lang.NullPointerException
at org.workflow.config.ActivitiConfiguration.runtimeService(ActivitiConfiguration.java:58)
at org.workflow.config.ActivitiConfiguration$$EnhancerByCGLIB$$41b535fe.CGLIB$runtimeService$4(<generated>)
at org.workflow.config.ActivitiConfiguration$$EnhancerByCGLIB$$41b535fe$$FastClassByCGLIB$$f20dd707.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:280)
at org.workflow.config.ActivitiConfiguration$$EnhancerByCGLIB$$41b535fe.runtimeService(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149)
… 13 more
</code>

jbarrez
Star Contributor
Star Contributor
Spring doesn't know the order in which to instantiate the beans, this is why the getProcessEngineBean() returns null.

To fix it, you need to use @DepensOn() or @Inject in a custom bean that wraps the runtimeservice.