protected void init() { initHistoryLevel(); initExpressionManager(); initVariableTypes(); initFormEngines(); initFormTypes(); initScriptingEngines(); initBusinessCalendarManager(); initCommandContextFactory(); initTransactionContextFactory(); initCommandExecutors(); initServices(); initIdGenerator(); initDeployers(); initJobExecutor(); initDataSource(); initTransactionFactory(); initSqlSessionFactory(); initSessionFactories(); initJpa(); } |
上面的各个初始化语句的作用如下:
1、initHistoryLevel() :初始化HistoryLevel,主要内容如下:
public void initHistoryLevel() { if (HISTORY_NONE.equalsIgnoreCase(history)) { historyLevel = 0; } else if (HISTORY_ACTIVITY.equalsIgnoreCase(history)) { historyLevel = 1; } else if (HISTORY_AUDIT.equalsIgnoreCase(history)) { historyLevel = 2; } else if (HISTORY_FULL.equalsIgnoreCase(history)) { historyLevel = 3; } else { throw new ActivitiException("invalid history level: "+history); } } |
2、initExpressionManager() :初始化ExpressionManager,主要内容如下:
protected void initExpressionManager() { if (expressionManager==null) { expressionManager = new ExpressionManager(); } } |
3、initVariableTypes() :初始化VariableTypes,主要内容如下:
protected void initVariableTypes() { if (variableTypes==null) { variableTypes = new DefaultVariableTypes(); if (customPreVariableTypes!=null) { for (VariableType customVariableType: customPreVariableTypes) { variableTypes.addType(customVariableType); } } variableTypes.addType(new NullType()); variableTypes.addType(new StringType()); variableTypes.addType(new BooleanType()); variableTypes.addType(new ShortType()); variableTypes.addType(new IntegerType()); variableTypes.addType(new LongType()); variableTypes.addType(new DateType()); variableTypes.addType(new DoubleType()); variableTypes.addType(new ByteArrayType()); variableTypes.addType(new SerializableType()); variableTypes.addType(new CustomObjectType("item", ItemInstance.class)); variableTypes.addType(new CustomObjectType("message", MessageInstance.class)); if (customPostVariableTypes!=null) { for (VariableType customVariableType: customPostVariableTypes) { variableTypes.addType(customVariableType); } } } } |
4、initFormEngines() :初始化FormEngines,主要内容如下:
protected void initFormEngines() { if (formEngines==null) { formEngines = new HashMap<String, FormEngine>(); FormEngine defaultFormEngine = new JuelFormEngine(); formEngines.put(null, defaultFormEngine); // default form engine is looked up with null formEngines.put(defaultFormEngine.getName(), defaultFormEngine); } if (customFormEngines!=null) { for (FormEngine formEngine: customFormEngines) { formEngines.put(formEngine.getName(), formEngine); } } } |
5、initFormTypes():
6、initScriptingEngines():
7、initBusinessCalendarManager():
8、initCommandContextFactory():初始化CommandContextFactory,主要内容如下:
protected void initCommandContextFactory() { if (commandContextFactory==null) { commandContextFactory = new CommandContextFactory(); commandContextFactory.setProcessEngineConfiguration(this); } } |
9、initTransactionContextFactory():
10、initCommandExecutors():初始化CommandExecutors,主要内容如下:
protected void initCommandExecutors() { initCommandInterceptorsTxRequired(); initCommandExecutorTxRequired(); initCommandInterceptorsTxRequiresNew(); initCommandExecutorTxRequiresNew(); } |
注意:从上面的四个方法的实现(源代码不在这里贴出)可以看出,“命令执行拦截器”可以增加自定义的拦截器,分别可以指定成预处理和后处理两部分,具体的变量为customPreCommandInterceptorsTxRequired、customPostCommandInterceptorsTxRequired、customPreCommandInterceptorsTxRequiresNew、customPostCommandInterceptorsTxRequiresNew。这些都是集合类型的变量,另外“命令执行拦截器”之所以要区分为“TxRequired”和“TxRequiresNew”还不是很清楚,从目前源代码中可以看出initIdGenerator()初始化的方法中采用了commandExecutorTxRequiresNew的“命令执行拦截器”的集合。拦截器的类结构图如下:
11、initServices():初始化工作流引擎的各个Service,主要内容如下:
protected void initServices() { protected void initService(Object service) { |
注意: 此处初始化只是为各个Service设置“命令执行拦截器”,并且该“命令执行拦截器”已经在initCommandExecutors()方法中初始化过了,另外从源代码中可以看出,每个Service的“命令执行者”都是由“LogInterceptor-->CommandContextInterceptor-->CommandExecutorImpl”组成的执行链状结构。注意,其中CommandContextInterceptor方法中对数据库操作的作用较大,要多关注。
12、initIdGenerator():初始化自增长序列生成器。
13、initDeployers():初始化部署者,具体代码如下:
protected void initDeployers() { if (deployers==null) { deployers = new ArrayList<Deployer>(); if (customPreDeployers!=null) { deployers.addAll(customPreDeployers); } deployers.addAll(getDefaultDeployers()); if (customPostDeployers!=null) { deployers.addAll(customPostDeployers); } } } |
14、initJobExecutor():初始化定时任务执行器
15、initDataSource():初始化数据源
16、initTransactionFactory():初始化事务工厂
17、initSqlSessionFactory():初始化sqlSessionFactory,主要是初始化iBatis的配置等相关信息,代码如下:
protected void initSqlSessionFactory() { // update the jdbc parameters to the configured ones... sqlSessionFactory = new DefaultSqlSessionFactory(configuration); } catch (Exception e) { |
18、initSessionFactories():初始化各个主要Service的SessionFactory
19、initJpa():初始化JPA