1. 程式人生 > >以activiti配置檔案為例介紹Spring管理的bean工廠

以activiti配置檔案為例介紹Spring管理的bean工廠

配置工廠Bean
通常由應用程式直接使用new建立新的物件,為了將物件的建立和使用相分離,採用工廠模式,即應用程式將物件的建立及初始化職責交給工廠物件.
一般情況下,應用程式有自己的工廠物件來建立bean.如果將應用程式自己的工廠物件交給Spring管理,那麼Spring管理的就不是普通的bean,而是工廠Bean.
得到物件的方式有兩種:
1.呼叫getBean()方法,Spring返回的不是直接建立的Bean的例項,而是由工廠Bean建立的Bean例項.
2.通過Spring的註解@Autowired得到配置檔案配置過的物件。

簡要分析下配置檔案

<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>
<!-- Activiti begin Activiti的核心工作物件processEngine依賴於這個類(SpringProcessEngineConfiguration), SpringProcessEngineConfiguration是核心配置檔案,通過這個物件載入一些配置檔案。 這個bean中有三個屬性: name:依賴的屬性的名稱,他決定著使用這個物件或者屬性的名稱。 value:依賴的這個屬性是一個單獨的屬性,不是物件,他的值是這個屬性的值。 ref:依賴的這個屬性是一個bean物件,這個bean物件在這個配置檔案中已經配置了這個bean,配置的id是ref的值。 --> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="FJMSDataSource"/> <property name="transactionManager" ref="transactionManager"/> <property name="databaseSchemaUpdate" value="true"/> <property name="jobExecutorActivate" value="false"/> <property name="history" value="full"/> <property name="processDefinitionCacheLimit" value="10"/> </bean> <!-- 這個是activiti的核心類,這個類依賴另一個類processEngineConfiguration --> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration"/> </bean> <!--通過ProcessEngineFactoryBean載入的物件可以得到以下這些物件。得到這些物件的方式是通過工廠模式得到的。 factory-method:通過工廠中的這個方法可以得到這個物件。 factory-bean:工廠依賴的bean物件,他的值是依賴的bean物件的id值。 --> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/> <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/> <bean id="formService" factory-bean="processEngine" factory-method="getFormService"/> <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/> <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/> <!-- Activiti end --> <context:annotation-config /> <!-- 對包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能--> <aop:aspectj-autoproxy /> <context:annotation-config /> <!-- 對包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能--> <aop:aspectj-autoproxy /> </beans>