81,122
社区成员




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;
}
}
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("--------------------------------------------------");
}
}
package com.test.bean;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Around("@annotation(com.test.annotation.Log)")
public Object aroundExce(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("我是LogAop_1");
System.out.println("我是Around,来打酱油的");
return joinPoint.proceed();
}