001/*
002 *  Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package com.mybatisflex.spring.datasource;
018
019import com.mybatisflex.annotation.UseDataSource;
020import org.aopalliance.aop.Advice;
021import org.springframework.aop.Pointcut;
022import org.springframework.aop.support.AbstractPointcutAdvisor;
023import org.springframework.aop.support.ComposablePointcut;
024import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
025import org.springframework.aop.support.annotation.AnnotationMethodMatcher;
026
027/**
028 * 多数据源切面。
029 *
030 * @author 王帅
031 * @since 2023-06-25
032 */
033public class DataSourceAdvice extends AbstractPointcutAdvisor {
034
035    private final Advice advice;
036    private final Pointcut pointcut;
037
038    public DataSourceAdvice() {
039        this.advice = new DataSourceInterceptor();
040
041        AnnotationMatchingPointcut cpc = new AnnotationMatchingPointcut(UseDataSource.class, true);
042        AnnotationMethodMatcher mpc = new AnnotationMethodMatcher(UseDataSource.class);
043        this.pointcut =  new ComposablePointcut(mpc).union(cpc);
044    }
045
046    @Override
047    public Pointcut getPointcut() {
048        return pointcut;
049    }
050
051
052    @Override
053    public Advice getAdvice() {
054        return this.advice;
055    }
056
057}