您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
Java Proxy代理在日志记录和管理中的应用可以通过静态代理和动态代理两种方式实现。
静态代理需要在编译时明确指定代理类。下面是一个简单的示例:
// 定义接口
public interface UserService {
void addUser(String name);
}
// 目标类
public class UserServiceImpl implements UserService {
@Override
public void addUser(String name) {
System.out.println("User added: " + name);
}
}
// 代理类
public class UserServiceProxy implements UserService {
private final UserService userService;
public UserServiceProxy(UserService userService) {
this.userService = userService;
}
@Override
public void addUser(String name) {
System.out.println("[LOG] Adding user...");
userService.addUser(name);
System.out.println("[LOG] User added.");
}
}
// 测试代码
public class StaticProxyDemo {
public static void main(String[] args) {
UserService userService = new UserServiceProxy(new UserServiceImpl());
userService.addUser("Alice");
}
}
动态代理在运行时动态生成代理类,更加灵活。下面是一个使用JDK动态代理的示例:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
// 定义接口
public interface UserService {
void addUser(String name);
void deleteUser(String name);
}
// 实现类
public class UserServiceImpl implements UserService {
@Override
public void addUser(String name) {
System.out.println("Added user: " + name);
}
@Override
public void deleteUser(String name) {
System.out.println("Deleted user: " + name);
}
}
// InvocationHandler实现类
public class UserServiceInvocationHandler implements InvocationHandler {
private final Object target;
public UserServiceInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Before method: " + method.getName());
Object result = method.invoke(target, args);
System.out.println("After method: " + method.getName());
return result;
}
}
// 测试代码
public class DynamicProxyDemo {
public static void main(String[] args) {
UserService userService = new UserServiceImpl();
UserService proxyInstance = (UserService) Proxy.newProxyInstance(
userService.getClass().getClassLoader(),
userService.getClass().getInterfaces(),
new UserServiceInvocationHandler(userService)
);
proxyInstance.addUser("John Doe");
proxyInstance.deleteUser("John Doe");
}
}
AOP(面向切面编程)可以通过自定义注解和切面实现更灵活的日志记录和管理:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
@Pointcut("@annotation(com.example.SyStemLog)")
public void logPointcut() {
}
@Around("logPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result;
try {
logger.info("Method {} is called", joinPoint.getSignature().getName());
result = joinPoint.proceed();
logger.info("Method {} execution completed", joinPoint.getSignature().getName());
} catch (Exception e) {
logger.error("Method {} execution failed", joinPoint.getSignature().getName(), e);
throw e;
}
return result;
}
}
// 自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SyStemLog {
String value() default "";
String description() default "";
String moduleName() default "";
String businessName() default "";
}
通过上述方法,可以在不修改业务类代码的情况下,实现日志记录和管理功能。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。