code manipulation : class proxy(cglib, bytebuddy)
This commit is contained in:
@@ -25,6 +25,19 @@
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>1.10.20</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.example.class_proxy;
|
||||
|
||||
public class Book {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String title;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.example.class_proxy;
|
||||
|
||||
public class BookService {
|
||||
public void rent(Book book) {
|
||||
System.out.println("rent: " + book.getTitle());
|
||||
}
|
||||
public void returnBook(Book book) {
|
||||
System.out.println("return: " + book.getTitle());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.example.class_proxy;
|
||||
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.implementation.InvocationHandlerAdapter;
|
||||
import net.bytebuddy.matcher.ElementMatchers;
|
||||
import net.sf.cglib.proxy.Enhancer;
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import net.sf.cglib.proxy.MethodProxy;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class BookServiceTest_ByteBuddy {
|
||||
|
||||
@Test
|
||||
public void proxy_byteBuddy() throws Exception {
|
||||
Class<? extends BookService> proxyClass = new ByteBuddy().subclass(BookService.class)
|
||||
.method(ElementMatchers.named("rent")).intercept(InvocationHandlerAdapter.of(new InvocationHandler() {
|
||||
BookService bookService = new BookService();
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
System.out.println("로깅!!");
|
||||
Object invoke = method.invoke(bookService, args);
|
||||
System.out.println("다른 작업!!");
|
||||
return invoke;
|
||||
}
|
||||
}))
|
||||
.make().load(BookService.class.getClassLoader()).getLoaded();
|
||||
BookService bookService = proxyClass.getConstructor(null).newInstance();
|
||||
|
||||
Book book = new Book();
|
||||
book.setTitle("spring");
|
||||
|
||||
bookService.rent(book);
|
||||
bookService.returnBook(book);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.example.class_proxy;
|
||||
|
||||
import net.sf.cglib.proxy.Enhancer;
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import net.sf.cglib.proxy.MethodProxy;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class BookServiceTest_CGlib {
|
||||
|
||||
@Test
|
||||
public void proxy_cglib() {
|
||||
MethodInterceptor handler = new MethodInterceptor() {
|
||||
BookService bookService = new BookService();
|
||||
@Override
|
||||
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
|
||||
if (method.getName().equals("rent")){
|
||||
System.out.println("로깅!!");
|
||||
Object invoke = method.invoke(bookService, args);
|
||||
System.out.println("다른 작업!!");
|
||||
return invoke;
|
||||
}
|
||||
return method.invoke(bookService, args);
|
||||
}
|
||||
};
|
||||
BookService bookService = (BookService) Enhancer.create(BookService.class, handler);
|
||||
|
||||
Book book = new Book();
|
||||
book.setTitle("spring");
|
||||
|
||||
bookService.rent(book);
|
||||
bookService.returnBook(book);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user