<!-- 整合hibernate -->
<context:component-scan base-package="service,dao"/>
<aop:aspectj-autoproxy/>
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
...
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
...
</bean>
<bean id="dataSource" class="xxx.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="ds1" value-ref="dataSource1"/>
<entry key="ds2" value-ref="dataSource2"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource1"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" lazy-init="false">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<!-- 每次读取的数目 -->
<prop key="hibernate.jdbc.fetch_size">100</prop>
<!-- 每次写入的数目 -->
<prop key="hibernate.jdbc.batch_size">30</prop>
</props>
</property>
<property name="packagesToScan" value="model"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" order="1"/>
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DataSourceHolder.getDataSourceType();
}
}
public class DataSourceHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
public static String getDataSourceType() {
return contextHolder.get();
}
public static void clearDataSourceType() {
contextHolder.remove();
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {
String value() default "";
}
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(0)
@Aspect
@Component
public class DataSourceAspect {
Logger log = LoggerFactory.getLogger(DataSourceAspect.class);
@Pointcut("execution(* service..*.*(..))")
private void myMethod() {}
@Around("myMethod()")
public Object aroundLog(ProceedingJoinPoint pjb) {
Object result = null;
Class<?> clazz = pjb.getTarget().getClass();
DataSource dataSource = clazz.getAnnotation(DataSource.class);
if(dataSource!=null){
String value = dataSource.value();
DataSourceHolder.setDataSourceType(value);
}
MethodSignature methodSignature = (MethodSignature) pjb.getSignature();
Method method = methodSignature.getMethod();
if(method.isAnnotationPresent(DataSource.class)){
dataSource = method.getAnnotation(DataSource.class);
String value = dataSource.value();
DataSourceHolder.setDataSourceType(value);
}
try {
result = pjb.proceed();
if(dataSource!=null){
DataSourceHolder.clearDataSourceType();
}
} catch (Throwable e) {
e.printStackTrace();
}
return result;
}
}