springboot:aop

===================================================
application.properties 增加aop选项
===================================================
# AOP
spring.aop.auto=true
spring.aop.proxy-target-class=true

如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。
如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理。
如果不给出 proxy-target-class,就按 proxy-target-class=“false”对待,也即是按JDK proxy来处理的。
===================================================
AopTest.java
[color=red]
<bean id="logService" class="org.spring.springboot.aop.AopTest"/>
<aop:config>
<aop:aspect id="log" ref="logService">
<aop:pointcut expression="execution(public * org.spring.springboot.web..*.*(..))" id="cLog"/>
<aop:before method="around" pointcut-ref="cLog"/>
<aop:before method="before" pointcut-ref="cLog"/>
<aop:after method="after" pointcut-ref="cLog"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="cLog" returning="object"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="cLog" arg-names="joinpoint,exception"
throwing="exception"/>
</aop:aspect>
</aop:config>
[/color]
===================================================
@Aspect
@Component
public class AopTest {

@Pointcut("execution(public * org.spring.springboot.web..*.*(..))")
public void webLog(){}

@Before("webLog()")
public void before(JoinPoint joinpoint) {

// 此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
Object[] args = joinpoint.getArgs();

if (args != null && args.length > 0) {

if (args[0] instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) args[0];
System.out.println("Request URI: " + request.getRequestURI());
}
}
}

@After("webLog()")
public void after(JoinPoint joinpoint) {
System.out.println("joinpoint = [" + joinpoint + "]");
}

@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
System.out.println("ret = [" + ret + "]");
}

@AfterThrowing(pointcut = "webLog()", argNames = "joinpoint,exception", throwing = "exception")
public void doAfterThrowing(JoinPoint joinpoint, Exception exception) {

String className = joinpoint.getTarget().getClass().getName();// 类名
String methodName = joinpoint.getSignature().getName();// 方法名字

String content = "--------------------------------reqlog. Exception error class "
+ className
+ "_"
+ methodName
+ "====== message ======"
+ exception.getMessage();
System.out.println(content);
System.out.println(exception);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值