一般在构建流程实例的时候, 会进行初始化,配置器就是可以在获取了流程配置实例,以及初始化之后可以进行一些额外的配置
在ProcessEngineConfigurationImpl这个类中调用init方法中initConfigurators方法,将所有流程配置器添加到allConfigurators这个集合,流程配置器有两种获取形式,第一种是直接通过bean方式注入到configurators遍历添加到allConfigurators集合,第二种是通过调用spi模式,加载到allConfigurators集合。最后将所有的按照照priority的大小进行排序
public void initConfigurators() {
//一个list集合,存放所有ProcessEngineConfigurator
allConfigurators = new ArrayList<ProcessEngineConfigurator>();
//遍历Configurators将其添加到allConfigurators这个集合
if (configurators != null) {
for (ProcessEngineConfigurator configurator : configurators) {
allConfigurators.add(configurator);
}
}
//获取当前类加载器
if (enableConfiguratorServiceLoader) {
ClassLoader classLoader = getClassLoader();
if (classLoader == null) {
classLoader = ReflectUtil.getClassLoader();
}
//利用spi模式加载ProcessEngineConfigurator所有实现类 将其添加到allConfigurators集合
ServiceLoader<ProcessEngineConfigurator> configuratorServiceLoader = ServiceLoader.load(ProcessEngineConfigurator.class, classLoader);
int nrOfServiceLoadedConfigurators = 0;
for (ProcessEngineConfigurator configurator : configuratorServiceLoader) {
allConfigurators.add(configurator);
nrOfServiceLoadedConfigurators++;
}
if (nrOfServiceLoadedConfigurators > 0) {
log.info("Found {} auto-discoverable Process Engine Configurator{}", nrOfServiceLoadedConfigurators++, nrOfServiceLoadedConfigurators > 1 ? "s" : "");
}
//将allConfigurators进行排序,按照priority1的从大到小
if (!allConfigurators.isEmpty()) {
Collections.sort(allConfigurators, new Comparator<ProcessEngineConfigurator>() {
@Override
public int compare(ProcessEngineConfigurator configurator1, ProcessEngineConfigurator configurator2) {
int priority1 = configurator1.getPriority();
int priority2 = configurator2.getPriority();
if (priority1 < priority2) {
return -1;
} else if (priority1 > priority2) {
return 1;
}
return 0;
}
});
// Execute the configurators
log.info("Found {} Process Engine Configurators in total:", allConfigurators.size());
for (ProcessEngineConfigurator configurator : allConfigurators) {
log.info("{} (priority:{})", configurator.getClass(), configurator.getPriority());
}
}
}
}
在ProcessEngineConfigurationImpl调用init方法中configuratorsBeforeInit方法,它会遍历所有的allConfigurators,并调用其中的beforeInit
/**调用所有配置器的beforeInit方法*/
public void configuratorsBeforeInit() {
for (ProcessEngineConfigurator configurator : allConfigurators) {
log.info("Executing beforeInit() of {} (priority:{})", configurator.getClass(), configurator.getPriority());
configurator.beforeInit(this);
}
}
在ProcessEngineConfigurationImpl调用init方法中configuratorsAfterInit方法,它会遍历所有的allConfigurators,并调用其中的configure
/**调用所有配置的configure方法*/
public void configuratorsAfterInit() {
for (ProcessEngineConfigurator configurator : allConfigurators) {
log.info("Executing configure() of {} (priority:{})", configurator.getClass(), configurator.getPriority());
configurator.configure(this);
}
}
public interface ProcessEngineConfigurator {
/**init方法的第二个方法调用,第一个是获取所有ProcessEngineConfigurator(当前接口的实例) 在初始化前做一些操作*/
void beforeInit(ProcessEngineConfigurationImpl processEngineConfiguration);
/**init方法的最后一个调用,在初始化后可以做一些配置*/
void configure(ProcessEngineConfigurationImpl processEngineConfiguration);
/**使用实例时,首先按照priority进行裴谞,如果你有实例之间的依赖性,自己定义优先级序*/
int getPriority();
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&characterEncoding=UTF-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password" value="fadp" />
</bean>
<bean id="processEngineConfigurator1" class="com.activiti_study.ch7.processEngineConfigurator1"></bean>
<bean id="processEngineConfigurator2" class="com.activiti_study.ch7.processEngineConfigurator2"></bean>
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="processEngineName" value="shareniu" />
<property name="configurators">
<list>
<ref bean="processEngineConfigurator1"/>
<ref bean="processEngineConfigurator2"/>
</list>
</property>
</bean>
</beans>
public class processEngineConfigurator1 extends AbstractProcessEngineConfigurator{
@Override
public void beforeInit(
ProcessEngineConfigurationImpl processEngineConfiguration) {
System.out.println("1:beforeInit ");
processEngineConfiguration.setDatabaseSchemaUpdate("true");
}
@Override
public void configure(
ProcessEngineConfigurationImpl processEngineConfiguration) {
System.out.println("1:configure ");
String databaseSchemaUpdate = processEngineConfiguration.getDatabaseSchemaUpdate();
System.out.println(databaseSchemaUpdate+"-----------------");
}
}
public class processEngineConfigurator2 extends AbstractProcessEngineConfigurator{
@Override
public void beforeInit(
ProcessEngineConfigurationImpl processEngineConfiguration) {
System.out.println("2:beforeInit ");
processEngineConfiguration.setDatabaseSchemaUpdate("true");
}
@Override
public void configure(
ProcessEngineConfigurationImpl processEngineConfiguration) {
System.out.println("2:configure ");
String databaseSchemaUpdate = processEngineConfiguration.getDatabaseSchemaUpdate();
System.out.println(databaseSchemaUpdate+"-----------------");
}
}
activit配置器的测试代码
public class App {
@Test
public void getProcessEngine() {
ProcessEngineConfiguration pec = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("com/activiti_study/ch7/activiti1.cfg.xml");
ProcessEngine pe1 = pec.buildProcessEngine();
}
}
1:beforeInit
2:beforeInit
1:configure
true-----------------
2:configure
true-----------------