Có thể tôi không suy nghĩ đủ hoặc câu trả lời thực sự khó nắm bắt. Kịch bản nhanh (Hãy thử mã. Nó biên dịch).CGLIB không thể chặn các phương thức trong một siêu lớp/siêu bề mặt
Xem xét một giao diện cũ
public interface LegacyInterfaceNoCodeAvailable{
void logInfo(String message);
}
Các xem xét việc thực hiện di sản của giao diện trên
public abstract class LegacyClassNoCodeAvailable implements LegacyInterfaceNoCodeAvailable{
public abstract void executeSomething();
public void rockItOldSchool(){
logInfo("bustin' chops, old-school style");
}
@Override
public void logInfo(String message){
System.out.println(message);
}
}
Bây giờ tôi đi vào như người đầy tham vọng này và viết một lớp cho hệ thống một 'mới' nhưng chạy bên trong khung 'Legacy', do đó tôi phải mở rộng lớp cơ sở cũ.
public class lass SpankingShiny extends LegacyClassNoCodeAvailable{
public void executeSomething(){
rockItOldSchool();
logInfo("I'm the King around here now");
System.out.println("this new stuff rocks!!");
}
}
Tất cả mọi thứ hoạt động tuyệt vời, giống như bạn mong chờ:
SpankingShiny shiny = new SpankingShiny();
shiny.executeSomething();
Lợi suất trên mã (như dự kiến):
bustin' chops, old-school style
I'm the King around here now
this new stuff rocks!!
Bây giờ khi bạn có thể thấy, 'System. out.println() 'trung thành in kết quả mong muốn. Nhưng tôi muốn thay thế 'System.out.println()' bằng trình ghi nhật ký.
Vấn đề:
Tôi không thể có proxy cglib chặn các phương pháp để 'logInfo (string)' và có nó in ra thông điệp của tôi mong muốn thông qua một logger (I have done cấu hình đăng nhập ngay bên cạnh đường). Đó là lời gọi phương thức 'rõ ràng' không nhấn proxy.
Code:
public class SpankingShinyProxy implements MethodInterceptor{
private SpankingShiny realShiny;
private final Logger logger = Logger.getLogger(SpankingShinyProxy.class);
public SpankingShinyProxy(SpankingShiny realShiny) {
super();
this.realShiny = realShiny;
}
@Override
public Object intercept(Object proxyObj, Method proxyMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable {
String methodName = proxyMethod.getName();
if("logInfo".equals(methodName)){
logger.info(methodParams[0]);
}
return proxyMethod.invoke(realShiny, methodParams);
}
public static SpankingShiny createProxy(SpankingShiny realObj){
Enhancer e = new Enhancer();
e.setSuperclass(realObj.getClass());
e.setCallback(new SpankingShinyProxy(realObj));
SpankingShiny proxifiedObj = (SpankingShiny) e.create();
return proxifiedObj;
}
}
phương pháp chính:
public static void main(String... args) {
SpankingShiny shiny = new SpankingShiny();
shiny.executeSomething();
SpankingShiny shinyO = SpankingShinyProxy.createProxy(shiny);
shinyO.executeSomething();
}
Sản lượng mã trên (KHÔNG như mong đợi):
bustin' chops, old-school style
I'm the King around here now
this new stuff rocks!!
bustin' chops, old-school style
I'm the King around here now
this new stuff rocks!!
đâu sẽ Tôi được đi sai?
Cảm ơn!