cancel
Showing results for 
Search instead for 
Did you mean: 

custom spring context loder

activitinike
Champ in-the-making
Champ in-the-making
Hello,

Please guide on setting custom spring context in activiti-app.war.

In SericeTask javaDelegate, we have many dependencies to inject (RestTemplate, some DAO etc..)


<context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
    </context-param> 

   <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener>


I tried to add spring context loader, but autowiring always return null onject.

Thanks for any help.
13 REPLIES 13

jbarrez
Star Contributor
Star Contributor
Which application are you talking about? Activiti 6? Enterprise?

Anyway, putting your configuration classes with the beans you want to expose in the component scanned packages should do the trick.

activitinike
Champ in-the-making
Champ in-the-making
We are using Enterprise.
Do you mean xml based spring configuration will not work as web.xml not read and can't load spring configuration?

We use activiti-app.war and custom jar containing service tasks as java delegate which has dependencies of custom DAOs, RestTemplate and more…


faizal-manan
Champ in-the-making
Champ in-the-making
what jbararez referring  is your spring configuration (applicationContext.xml). In simple word, Spring can't find your ServiceTask and you need to tell Spring in which package your ServiceTask is located. You can use unit test (create new if not exists) to see either your applicationContext.xml properly configured or not.

Issue is, it seems spring application context itself is not loaded in activiti-app.war.

According to below thread, web.xml is not read, and I cant find any default or existing spring configuration file.
https://forums.activiti.org/content/custom-spring-context


<code>
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
       
  <context:annotation-config/>

  <context:component-scan base-package="com.myservicetask.package"/>

<!– RestTemplate definition for autowiring –>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate" />

</beans>
</code>

jbarrez
Star Contributor
Star Contributor
Question on Enterprise -> please use the Enterprise forum, hence my question.

The Enterprise version uses JavaConfig and no XML. Also the web.xml is bare minimum and only needed to bootup the Spring container. To add custom logic, use for example https://docs.alfresco.com/activiti/docs/dev-guide/1.4.0/#_spring_beans

activitinike
Champ in-the-making
Champ in-the-making
Thanks Joram,

It works with https://docs.alfresco.com/activiti/docs/dev-guide/1.4.0/#_spring_beans

Is it possible to use our custom package instead of : com.activiti.extension.bean and com.activiti.extension.conf ?

I tried using custom packages with @ComponentScan annotation, but it always fails with:
<code> org.activiti.engine.impl.javax.el.PropertyNotFoundException: Cannot resolve identifier </code>

Thanks for any further assistance.

If possible, kindly move this topic to Enterprise forum please?

jbarrez
Star Contributor
Star Contributor
Yes, simply add a new @Configuration in the .conf package that has a component scan for your custom packages.

jonnyg
Confirmed Champ
Confirmed Champ
In other words, this is what we should do:

<java>
package com.sample.bpm.conf;

import com.sample.bpm.bean.SampleBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackageClasses={SampleConfig.class, SampleBean.class})
public class SampleConfig{

}

</java>


jbarrez
Star Contributor
Star Contributor
Almost: the @Configuration bean should be be discoverable by the default component scan, so you need to put it in the com.activiti.extension.conf package.

The @ComponentScan on that bean can point to any of the packages you'd like to scan additionally.