[java] Byte Buddy를 사용하여 동적 프록시를 활용하는 예시는?
다음은 Byte Buddy를 사용하여 동적 프록시를 활용하는 예시입니다.
먼저, Byte Buddy 라이브러리를 프로젝트에 추가해야 합니다. Maven을 사용한다면 pom.xml
파일에 다음 의존성을 추가할 수 있습니다.
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.10.22</version>
</dependency>
이제 실제로 동적 프록시를 생성하는 코드를 작성해 보겠습니다.
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.reflect.Method;
public class ExampleProxyGenerator {
public static <T> T createProxy(Class<T> targetClass, Object interceptor) {
try {
return new ByteBuddy()
.subclass(targetClass)
.method(ElementMatchers.isDeclaredBy(targetClass))
.intercept(MethodDelegation.to(interceptor))
.make()
.load(targetClass.getClassLoader())
.getLoaded()
.getConstructor()
.newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to create proxy", e);
}
}
}
public interface ExampleService {
String greet(String name);
}
public class ExampleServiceImpl implements ExampleService {
public String greet(String name) {
return "Hello, " + name;
}
}
public class LoggingInterceptor {
public static String intercept(@SuperCall Callable<String> superMethod, @AllArguments Object[] args) throws Exception {
System.out.println("Method " + superMethod.getMethodName() + " is called with arguments " + Arrays.toString(args));
String result = superMethod.call();
System.out.println("Result: " + result);
return result;
}
}
public class Main {
public static void main(String[] args) {
ExampleService proxy = ExampleProxyGenerator.createProxy(ExampleService.class, new LoggingInterceptor());
System.out.println(proxy.greet("John"));
}
}
위 코드에서는 ExampleService
인터페이스를 구현하는 ExampleServiceImpl
클래스를 대상으로 동적 프록시를 생성합니다. 동적 프록시는 메소드 호출을 LoggingInterceptor
클래스로 위임하여 호출 전후에 로깅 작업을 수행합니다.
ExampleProxyGenerator
클래스의 createProxy
메소드에서는 ByteBuddy
를 사용하여 동적 프록시 클래스를 생성하고, MethodDelegation.to(interceptor)
를 통해 인터셉터를 등록합니다. 이후 생성된 동적 프록시 클래스는 클래스로더에 로드되고, 인스턴스를 생성하여 반환합니다.
위 예시에서 Main
클래스에서 ExampleService
인터페이스로 정의된 proxy
인스턴스를 생성하고 greet
메소드를 호출하면, LoggingInterceptor
클래스의 intercept
메소드가 실행되어 로깅이 출력됩니다.
이처럼 Byte Buddy를 사용하여 동적인 프록시를 생성하고 활용할 수 있습니다. 더 복잡한 경우에는 Byte Buddy의 다양한 기능을 사용하여 메소드 호출 변경, 인터페이스 구현, 어노테이션 적용 등 다양한 작업을 수행할 수 있습니다.