java开发经常会用到注解与切面
今天想在实例与切点中获取(自己,接口,继承类)的method的注解
简单的一个测试注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HelloA {
String name() default "hello";
}
public interface ITest {
String dd = "sdd";
@HelloA(name = "twpI")
String test();
}
public abstract class FatherTest {
@HelloA(name = "fatherTest")
public abstract String test();
}
main测试
public class Test extends FatherTest implements ITest {
@Override
@HelloA(name = "MineTest")
public String test() {
return "ss";
}
public static void main(String[] args){
FatherTest i = new Test();
try {
HelloA helloAmine= i.getClass().getMethod("test",null).getAnnotation(HelloA.class);
helloAmine.name();
HelloA helloAFather = i.getClass().getSuperclass().getMethod("test",null).getAnnotation(HelloA.class);
HelloA helloAInterface = i.getClass().getInterfaces()[0].getMethod("test",null).getAnnotation(HelloA.class);
helloAFather.name();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
//i.getClass().getAnnotations()
}
}
调试面板
其实关于切点的注解获取,要简单的多了
直接上代码
//第三方退款的切点01
//适配器
@Pointcut("execution(* com.efivestar.upg.adapter.*.refund(..))")
private void pointsForAdapter() {
}
//第三方退款的切点02
// callback
@Pointcut(value = "execution(* com.efivestar.upg.web.callback.*.refund(..))&& args(request,response)",argNames="request,response")
private void pointsForCallBack(HttpServletRequest request,HttpServletResponse response) {
}
@Around(argNames = "point,request,response",value = "pointsForCallBack(request,response)")
public void UpdateTaskRefundRemoteState(ProceedingJoinPoint point, HttpServletRequest request,HttpServletResponse response){
try {
Object[] args = point.getArgs();
point.proceed(args);
//String returnstr = point.proceed(args).toString();
// success代表对第三方 跟 对业务系统都是成功的
//if (returnstr.toLowerCase().equals("success".toLowerCase())) return;
Optional<TaskIdentify> taskIdentifyOptional = getTaskIdentify(point);
if(!taskIdentifyOptional.isPresent()) return;
TaskIdentify taskIdentify = taskIdentifyOptional.get();
//处理失败的情况
// 查询订单的状态判断是remote还是service的错误中断
TradeOrder tradeOrder = tradeOrderService.queryTradeOrderByGwTradeNo(request.getParameter("batch_no"));
//判断任务类型
// boolean remoteStatus = (tradeOrder.getStatus().intValue() == Constants.已退款订单.intValue()
// || tradeOrder.getStatus().intValue() == Constants.退款异常 )?true:false;
Optional<ITaskHandler> handlerOptional = MatchTaskToHandler(tradeOrder,taskIdentify);
if (!handlerOptional.isPresent()) return;
ITaskHandler handler = handlerOptional.get();
handler.JoinTask(new TradeOrderTask(){
{
//看枚举配置中是否需要想第三方请求状态
setStatusRemote(true);
setTradeOrderId(tradeOrder.getId());
Date d = new Date();
setCreateTime(d);
setUpdateTimeService(d);
setUpdateTimeRemote(d);
setExecuteRemote(new Date(d.getTime()+ handler.delayTime));
setExecuteService(new Date(d.getTime() + handler.delayTime));
setIdentify(taskIdentify.getOperation());
}
});
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@AfterReturning(returning = "returnObject", pointcut = "pointsForAdapter()")
public void JoinPollTaskRefund(JoinPoint joinPoint,ReturnObject returnObject){
try {
Optional<TaskIdentify> taskIdentifyOptional = getTaskIdentify(joinPoint);
// 如果不需要加入任务的话,就退出方法
//if (!taskIdentify.isProcessAop()) return;
// if (taskRefundList.size()==0) return;
// 只拿去ReFund的task
// TaskIdentify taskIdentify = taskRefundList.get(0);
if(!taskIdentifyOptional.isPresent()) return;
TaskIdentify taskIdentify = taskIdentifyOptional.get();
//做轮询任务加入
ReturnRefundObject returnRefundObject =
JSON.parseObject(returnObject.getReturnData(),ReturnRefundObject.class);
// 如果任务失败了的话,不加入任务
if(!returnRefundObject.getResult().equals(Constants.SUCCESS)) return;
TradeOrder refundTrade = tradeOrderService.queryTradeOrderByGwTradeNo(returnRefundObject.getGwTradeNo());
Optional<ITaskHandler> handlerOptional = MatchTaskToHandler(refundTrade,taskIdentify);
if (!handlerOptional.isPresent()) return;
// 如果这种方式由用户发起,将加入在callback去
// 在callback处理他的returnrefundobject
if(taskIdentify.isOperationUpToUser()) return;
ITaskHandler handler = handlerOptional.get();
handler.JoinTask(new TradeOrderTask(){
{
//看枚举配置中是否需要想第三方请求状态
setStatusRemote(!taskIdentify.isQueryThird());
setTradeOrderId(refundTrade.getId());
Date d = new Date();
setCreateTime(d);
setUpdateTimeService(d);
setUpdateTimeRemote(d);
setExecuteRemote(new Date(d.getTime()+ handler.delayTime));
setExecuteService(new Date(d.getTime() + handler.delayTime));
setIdentify(taskIdentify.getOperation());
}
});
}catch (Exception e){
log.error("error:",e.toString());
}
}