spring中同时注解responseBody和自定义aop,但是aop不起不作用

道秋adol 道秋adol 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下
controller代码如下:

package com.test.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.annotation.Log;
import com.test.bean.User;
import com.test.service.IUserService;

@Controller
@RequestMapping("/test")
public class TestController {

@Autowired
private IUserService userService;


@RequestMapping("/aop1")
@Log(name = "你访问的你aop1方法")
@ResponseBody
public String aop1(HttpServletResponse response) throws IOException {
System.out.println("调用action aop1");
// response.setHeader("Content-Type", "text/html;charset=UTF-8");
// response.getWriter().print("apo1");
// response.getWriter().flush();
return "aop1";
}

@Log(name = "您访问了aop2方法")
@RequestMapping(value = "aop2")
@ResponseBody
public String aop2(String string) throws InterruptedException {
System.out.println("调用action aop2");
Thread.sleep(1000L);
User user = new User();
user.setName(string);
userService.save(user);
return string;
}
}


自定义Log代码

package com.test.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
@Documented
@Inherited
public @interface Log {
String name() default "";
}


package com.test.aop;

import java.lang.reflect.Method;
import java.util.UUID;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.test.annotation.Log;

@Aspect
@Component
public class LogAop_1 {

ThreadLocal<Long> time = new ThreadLocal<Long>();

ThreadLocal<String> tag = new ThreadLocal<String>();

/**
* 在所有标注@Log的地方注入
*
* @param joinPoint
* */
@Before("@annotation(com.test.annotation.Log)")
public void beforeExce(JoinPoint joinPoint) {
time.set(System.currentTimeMillis());
tag.set(UUID.randomUUID().toString());
info(joinPoint);
MethodSignature ms=(MethodSignature) joinPoint.getSignature();
Method method=ms.getMethod();
System.out.println(method.getAnnotation(Log.class).name()+"标记"+tag.get());
}

@After("@annotation(com.test.annotation.Log)")
public void afterExce(JoinPoint joinPoint){
MethodSignature ms=(MethodSignature) joinPoint.getSignature();
Method method=ms.getMethod();
System.out.println("标记为"+tag.get()+"的方法"+method.getName()+"运行消耗"+(System.currentTimeMillis()-time.get())+"ms");
}

@Around("@annotation(com.test.annotation.Log)")
public void aroundExce(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("我是LogAop_1");
System.out.println("我是Around,来打酱油的");
joinPoint.proceed();
}
private void info(JoinPoint joinPoint) {
System.out.println("--------------------------------------------------");
System.out.println("King:\t" + joinPoint.getKind());
System.out.println("Target:\t" + joinPoint.getTarget().toString());
Object[] os = joinPoint.getArgs();
System.out.println("Args:");
for (int i = 0; i < os.length; i++) {
System.out.println("\t==>参数[" + i + "]:\t" + os[i].toString());
}
System.out.println("Signature:\t" + joinPoint.getSignature());
System.out.println("SourceLocation:\t" + joinPoint.getSourceLocation());
System.out.println("StaticPart:\t" + joinPoint.getStaticPart());
System.out.println("--------------------------------------------------");
}
}

其中的mode

package com.test.bean;

public class User {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
道秋adol
Web 开发 81,107 成员 341,720 内容