使用aop管理日志
程序员文章站
2022-03-02 15:40:13
...
使用aop管理日志
创建实体类User.java
package com.zr.aop.pojo;
public class User {
private String id;
private String userName;
private Integer age;
public User(String id, String userName, Integer age) {
this.id = id;
this.userName = userName;
this.age = age;
}
public User() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", userName='" + userName + '\'' +
", age=" + age +
'}';
}
}
创建UserService.java及其实现类UserServiceImpl.java
package com.zr.aop.service;
import com.zr.aop.pojo.User;
public interface UserService {
void printUser(User user);
}
package com.zr.aop.service.impl;
import com.zr.aop.anno.Log;
import com.zr.aop.pojo.User;
import com.zr.aop.service.UserService;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class UserServiceImpl implements UserService {
@Override
@Log(OperateType = "print",OperateName = "打印...")
public void printUser(User user) {
if(Objects.isNull(user)){
throw new RuntimeException("不能为空哟...");
}
System.out.println("用户ID:"+user.getId()+",用户名:"+user.getUserName()+",用户年龄:"+user.getAge());
}
}
自定义注解Log.class
package com.zr.aop.anno;
import java.lang.annotation.*;
@Target({ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
public String OperateType() default "";
public String OperateName() default "";
}
创建AOP
package com.zr.aop.aop;
import com.zr.aop.anno.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(* com.zr.aop.service.impl.UserServiceImpl.printUser(..))")
public void pointCut(){
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) throws Exception{
String clzName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
Class<?> clz = Class.forName(clzName);
Method[] methods = clz.getMethods();
for(Method method:methods){
if(method.getName().equals(methodName)){
String operateType = method.getAnnotation(Log.class).OperateType();
String operateName = method.getAnnotation(Log.class).OperateName();
System.out.println(operateName+":::"+operateType);
}
}
System.out.println(clzName+":"+methodName+":"+args[0].toString());
}
@After("pointCut()")
public void after(JoinPoint joinPoint){
System.out.println("方法执行结束.....");
}
@Around("pointCut()")
public void around(ProceedingJoinPoint joinPoint){
System.out.println("环绕通知开始====================");
long start = System.currentTimeMillis();
try {
joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("环绕通知结束================"+(end-start));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@AfterThrowing(pointcut = "pointCut()",throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint,Throwable e){
String clzName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
System.out.println("出现异常了,类:"+clzName+",方法:"+methodName);
}
}
测试
@Test
public void test(){
User user = new User("1","david",27);
userService.printUser(user);
}