xml文件配置
<context:component-scan base-package="com.silencer.web" />
<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true" />
新建注解
@Documented()
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
// 方法说明
String value();
}
定义切面类
@Aspect
@Component
public class LogAspect {
private static Logger logger = LoggerFactory.getLogger(LogAspect.class);
private long start;
@Before("@annotation(log)")
public void doBefore(JoinPoint point, Log log) {
this.start = System.currentTimeMillis();
logger.info("---------> {} begin! start time: {} <---------", log.value(), start);
}
@After("@annotation(log)")
public void doAfter(JoinPoint point, Log log) {
long cost = System.currentTimeMillis() - start;
logger.info("---------> {} end!, use time: {}", log.value(), cost);
}
}
此处以注解的方式定义aop切面,@annotation(log)中的log和参数中的log相同,此处拦截@Log注解
切面表达式
匹配service包中的所有类及其子包中的类:within(com.silencer.web.service..*)
匹配service包中的任意类:within(com.silencer.web.service.*)
execution写到方法级:execution(* com.silencer.web.service.*.impl.*.*(..))
@annotation拦截自定义注解:@annotation(com.silencer.web.utils.annotation.Log)或@annotation(log),log为方法中的参数
可以用
@Aspect
@Component
public class LogAspect {
private static Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Pointcut("within(com.silencer.web.service..*)")
public void pointcut() {
}
private long start;
@Before("pointcut() && @annotation(log)")
public void doBefore(JoinPoint point, Log log) {
this.start = System.currentTimeMillis();
logger.info("---------> {} begin! start time: {} <---------", log.value(), start);
}
@After("@annotation(log)")
public void doAfter(JoinPoint point, Log log) {
long cost = System.currentTimeMillis() - start;
logger.info("---------> {} end!, use time: {}", log.value(), cost);
}
}
pointcut可以用 &&、||、!的方式进行组合
切面表达式
@within获取类上的注解,@annotation获取方法上的注解
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface DataSource {
DataSourceEnum value() default DataSourceEnum.DEFAULT;
}
@Aspect
@Component
public class DataSourceAspect {
private static Logger log = LoggerFactory.getLogger(DataSourceAspect.class);
/**
* TODO @within拦截类上的注释,@anntoation拦截方法上的注释
* <p> 参数中的dataSource只会去@annotation的值
*
* @param point
* @param dataSource
*/
@Before("@within(dataSource) || @annotation(dataSource)")
public void doBefore(JoinPoint point, DataSource dataSource) {
if (ObjectUtils.isEmpty(dataSource)) {
dataSource = point.getTarget().getClass().getAnnotation(DataSource.class);
}
DynamicDataSource.setCustomerType(dataSource.value());
log.info("---------> {} <---------", dataSource.value());
}
@After("@within(dataSource) || @annotation(dataSource)")
public void doAfter(JoinPoint point, DataSource dataSource) {
// 数据库设置为默认数据库
DynamicDataSource.resetDefaultType();
}
}