mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: 修复在 Controller 通过注解配置数据源不生效的问题
This commit is contained in:
parent
0e750622f6
commit
3b65108f0f
@ -32,8 +32,9 @@ import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* <p>判断是否有 MyBatis-Flex 的多数据源配置。
|
||||
*
|
||||
* <p>如果配置文件中有 MyBatis-Flex 的多数据源配置,就加载 MyBatis-Flex 多数据源自动配置类。
|
||||
*
|
||||
* @author michael
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@ -19,6 +19,7 @@ import com.mybatisflex.core.mybatis.FlexConfiguration;
|
||||
|
||||
/**
|
||||
* 为 {@link FlexConfiguration} 做自定义的配置支持。
|
||||
* @author michael
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ConfigurationCustomizer {
|
||||
|
||||
@ -28,6 +28,7 @@ import org.springframework.transaction.annotation.TransactionManagementConfigure
|
||||
|
||||
/**
|
||||
* MyBatis-Flex 事务自动配置。
|
||||
* @author michael
|
||||
*/
|
||||
@ConditionalOnClass(Db.class)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
|
||||
@ -17,12 +17,8 @@ package com.mybatisflex.spring.boot;
|
||||
|
||||
import com.mybatisflex.core.datasource.DataSourceBuilder;
|
||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||
import com.mybatisflex.spring.datasource.DataSourceAdvice;
|
||||
import com.mybatisflex.spring.datasource.DataSourceInterceptor;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@ -30,7 +26,6 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Role;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Map;
|
||||
@ -38,6 +33,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* MyBatis-Flex 多数据源的配置支持。
|
||||
* @author michael
|
||||
*/
|
||||
@ConditionalOnMybatisFlexDatasource()
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ -73,14 +69,4 @@ public class MultiDataSourceAutoConfiguration {
|
||||
return flexDataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link com.mybatisflex.annotation.UseDataSource} 注解切换数据源切面。
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
public Advisor dataSourceAdvice() {
|
||||
return new DataSourceAdvice(new DataSourceInterceptor());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -28,6 +28,7 @@ import com.mybatisflex.core.table.TableManager;
|
||||
import com.mybatisflex.core.tenant.TenantFactory;
|
||||
import com.mybatisflex.core.tenant.TenantManager;
|
||||
import com.mybatisflex.spring.FlexSqlSessionFactoryBean;
|
||||
import com.mybatisflex.spring.datasource.DataSourceAdvice;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.mapping.DatabaseIdProvider;
|
||||
import org.apache.ibatis.plugin.Interceptor;
|
||||
@ -41,6 +42,7 @@ import org.mybatis.spring.mapper.MapperFactoryBean;
|
||||
import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.factory.*;
|
||||
@ -55,10 +57,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandi
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
@ -414,4 +413,16 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@link com.mybatisflex.annotation.UseDataSource} 注解切换数据源切面。
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
public Advisor dataSourceAdvice() {
|
||||
return new DataSourceAdvice();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -32,19 +32,23 @@ import org.springframework.aop.support.annotation.AnnotationMethodMatcher;
|
||||
*/
|
||||
public class DataSourceAdvice extends AbstractPointcutAdvisor {
|
||||
|
||||
private final DataSourceInterceptor advice;
|
||||
private final Advice advice;
|
||||
private final Pointcut pointcut;
|
||||
|
||||
public DataSourceAdvice(DataSourceInterceptor advice) {
|
||||
this.advice = advice;
|
||||
public DataSourceAdvice() {
|
||||
this.advice = new DataSourceInterceptor();
|
||||
|
||||
AnnotationMatchingPointcut cpc = new AnnotationMatchingPointcut(UseDataSource.class, true);
|
||||
AnnotationMethodMatcher mpc = new AnnotationMethodMatcher(UseDataSource.class);
|
||||
this.pointcut = new ComposablePointcut(mpc).union(cpc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pointcut getPointcut() {
|
||||
AnnotationMatchingPointcut cpc = new AnnotationMatchingPointcut(UseDataSource.class, true);
|
||||
AnnotationMethodMatcher mpc = new AnnotationMethodMatcher(UseDataSource.class);
|
||||
return new ComposablePointcut(cpc).union(mpc);
|
||||
return pointcut;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Advice getAdvice() {
|
||||
return this.advice;
|
||||
|
||||
@ -43,7 +43,6 @@ public class DataSourceInterceptor implements MethodInterceptor {
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
|
||||
String dsKey = DataSourceKey.get();
|
||||
if (StringUtil.isNotBlank(dsKey)) {
|
||||
return invocation.proceed();
|
||||
|
||||
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.mybatisflex.test.controller;
|
||||
|
||||
import com.mybatisflex.annotation.UseDataSource;
|
||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||
import com.mybatisflex.core.paginate.Page;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.row.Db;
|
||||
@ -31,6 +33,7 @@ import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@UseDataSource("ds3333")
|
||||
public class AccountController {
|
||||
|
||||
@Resource
|
||||
@ -106,4 +109,12 @@ public class AccountController {
|
||||
return accountMapper.paginate(pageNumber, pageSize, QueryWrapper.create());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/ds")
|
||||
@UseDataSource("ds2222")
|
||||
public String ds() {
|
||||
return ">>>>>ds: " + DataSourceKey.get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user