将activiti的一个xml配置文件转换成配置类

在activiti的使用中,遇到了一个xml配置文件,将其改成一个配置类,更加符合springboot的文件布局!
首先是xml文件的内容:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/contex http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--数据源配置dbcp-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://****:3306/activiti7"/>
        <property name="username" value="****"/>
        <property name="password" value="****"/>
    </bean>
    <!--activiti单独运行的ProcessEngine配置对象(processEngineConfiguration),使用单独启动方式
        默认情况下:bean的id=processEngineConfiguration
    -->
    <!--注意脱机方式使用的类是org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration不是org.activiti.engine.ProcessEngineConfiguration-->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource"></property>
        <!--activiti数据库表处理策略-->
        <property name="databaseSchemaUpdate" value="true"/>
        <!-- 可以注入多个类到activiti的beans中,其中key对应的就是我们的类名 -->
        <property name="beans">
            <map>
                <entry key="commonmethod" value-ref="commonmethod"/>
            </map>
        </property>
    </bean>
    <bean class="com.jingchuang.mes.activiti.CommonMethod" id="commonmethod"></bean>
</beans>

分析一下这个文件:文件中的<beans>标签的内容是不需要被转换成类或者方法注入的!这个文件中需要注入的内容一共有三个,他们的ID分别是:

  • dataSource
  • processEngineConfiguration
  • commonmethod
    下面开始编写同等作用的配置类:
    首先新建一个类ActivityConfig,这个类,相当于这个xml文件本身,我们注入的bean卸载这个类中。先在此类中注入一个DataSource,并将一些参数使用@Value注解获取(这里理解所谓的注入就是返回一个类的对象),在xml文件中bean的class类型为org.springframework.jdbc.datasource.DriverManagerDataSource,所以我们需要返回的类型为这个类的一个对象。
@Configuration
public class ActivityConfig {
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String passwd;
    @Bean(name = "dataSource")
    public DataSource dataSource() {
        DriverManagerDataSource ds = new DriverManagerDataSource();
        try {
            ds.setDriverClassName("com.mysql.jdbc.Driver");
            ds.setUrl(url);
            ds.setUsername(username);
            ds.setPassword(passwd);
            return ds;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    }

然后是第二个bean标签processEngineConfiguration,这个标签内有三个属性,一个是dataSource(上面已经注入过了),一个是databaseSchemaUpdate,默认值是String类型的true字符,还有一个是map类型。map中存了一个CommonMethod的对象,是从第三个bean那里引用的。所以这里先把第三个bean写出。新建一个类CommonMethod.java

@Component
public class CommonMethod {
    @Autowired
    private UserService userService;
    @Bean(name="commonmethod")
    public CommonMethod getCommonMethod(){
        return new CommonMethod();
    }
    public String getUserNameByUserId(int userId){
        User user = userService.get(userId);
        return user.getLoginName();
    }
}

然后补全前面第二个bean:

@Resource(name = "commonmethod")
    private CommonMethod commonmethod;
    
@Bean(name = "processEngineConfiguration")
    public StandaloneProcessEngineConfiguration processEngineConfiguration() {
        StandaloneProcessEngineConfiguration stdConfig = new StandaloneProcessEngineConfiguration();
        Map bean = new HashMap();
        bean.put("commonmethod", commonmethod);
        try {
            stdConfig.setDataSource(dataSource());
            stdConfig.setDatabaseSchemaUpdate("true");
            stdConfig.setBeans(bean);
            return stdConfig;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

完整的配置类ActivityConfig.class

@Configuration
public class ActivityConfig {

    @Resource(name = "commonmethod")
    private CommonMethod commonmethod;
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String passwd;

    @Bean(name = "dataSource")
    public DataSource dataSource() {
        DriverManagerDataSource ds = new DriverManagerDataSource();
        try {
            ds.setDriverClassName("com.mysql.jdbc.Driver");
            ds.setUrl(url);
            ds.setUsername(username);
            ds.setPassword(passwd);
            return ds;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Bean(name = "processEngineConfiguration")
    public StandaloneProcessEngineConfiguration processEngineConfiguration() {
        StandaloneProcessEngineConfiguration stdConfig = new StandaloneProcessEngineConfiguration();
        Map bean = new HashMap();
        bean.put("commonmethod", commonmethod);
        try {
            stdConfig.setDataSource(dataSource());
            stdConfig.setDatabaseSchemaUpdate("true");
            stdConfig.setBeans(bean);
            return stdConfig;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值