hap官方文档地址;http://eco.hand-china.com/doc/hap/latest/dev_guide/01.getting_start/09_multi_data_source.html
官方文档没有讲解hap默认配置,只是讲了怎么配置多数据源,对于我第一次配置的时候,不免遇到很多问题,下面是我对问题的描述跟解决方案
- 添加配置文件
Hap多数据源 是已mapper作为维度来配置的,即mapper下的方法都用一个数据源
hap默认配置单数据源,配置文件为applicationContext.xml文件,
故我们使用配置的时候,尽量避免用此文件名,否则会导致hap很多类都初始化加载不对,从而导致启动报错
我们可以看WEB-INF/classes/spring/applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<context:property-placeholder location="classpath:config.properties"/>
<bean id="coreApplicationListener" class="com.hand.hap.core.ApplicationListenerBean"/>
<!-- middleware datasource com.hand.hap.core.JndiObjectFactoryBean-->
<!-- jndi dataSource-->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${db.jndiName}"/>
</bean>
<!-- builtin datasource -->
<!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!--<property name="driverClassName"-->
<!--value="com.mysql.jdbc.Driver"/>-->
<!--<property name="url" value="jdbc:mysql://127.0.0.1:3306/hap_dev"/>-->
<!--<property name="username" value="hap_dev"/>-->
<!--<property name="password" value="hap_dev"/>-->
<!--</bean>-->
<bean id="languageProvider" class="com.hand.hap.core.impl.LanguageProviderImpl"/>
<bean id="messageSource" class="com.hand.hap.core.i18n.CacheMessageSource"/>
<!-- config transactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:/**/*Mapper.xml"/>
<property name="plugins">
<array>
<bean class="com.hand.hap.core.interceptor.RequestContextInterceptor"/>
<bean class="com.hand.hap.core.interceptor.MultiLanguageInterceptor"/>
<bean class="com.hand.hap.core.interceptor.SecurityTokenInterceptor"/>
<bean class="com.hand.hap.core.interceptor.OvnInterceptor"/>