以activiti配置文件为例介绍Spring管理的bean工厂

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33824312/article/details/79021505

配置工厂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>

猜你喜欢

转载自blog.csdn.net/qq_33824312/article/details/79021505
今日推荐