springAOP的使用

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();
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值