package com.cheche365.dictonary.datatrans.datatrans.aop;
import com.cheche365.dictonary.datatrans.datatrans.annotion.DyDataSource;
import com.cheche365.dictonary.datatrans.datatrans.config.DataSourceHolder;
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.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanExpressionContext;
import org.springframework.beans.factory.config.BeanExpressionResolver;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.expression.StandardBeanExpressionResolver;
import org.springframework.stereotype.Component;
/**
* @author sunyan
* @date 2022/11/21 15:38
* @description
*/
@Component
@Aspect
public class DataSourceAop {
private BeanExpressionResolver resolver;
private BeanExpressionContext expressionContext;
private ConfigurableBeanFactory beanFactory;
public DataSourceAop(ConfigurableBeanFactory beanFactory) {
this.beanFactory = beanFactory;
this.resolver = beanFactory.getBeanExpressionResolver();
this.expressionContext = new BeanExpressionContext(beanFactory,
null);
}
@Pointcut("@annotation(com.cheche365.dictonary.datatrans.datatrans.annotion.DyDataSource)")
public void pointCut(){
}
@Around("pointCut()&&@annotation(dataSource)")
public Object around( ProceedingJoinPoint joinPoint,DyDataSource dataSource) throws Throwable {
String dataSourceName = dataSource.value();
dataSourceName = (String) resolver.evaluate(resolve(dataSourceName),expressionContext);
Object result = joinPoint.proceed();
return result;
}
/**
* Resolve the specified value if possible.
* @param value the value to resolve
* @return the resolved value
* @see ConfigurableBeanFactory#resolveEmbeddedValue
*/
private String resolve(String value) {
if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory) {
return this.beanFactory.resolveEmbeddedValue(value);
}
return value;
}
}
spring boot自定义注解使用spel表达式
最新推荐文章于 2024-02-26 17:29:26 发布