オブジェクトを生成し、すべてのパブリックメソッドをインターセプトするファクトリクラスを実装しようとしています。
ここで2つのメソッドを呼び出そうとしています。1:すでに呼び出されたメソッド2:私のベースのメソッド。どうすればこれを達成できるか考えていますか?
public class LoggerFactory {
public LoggerFactory() {
}
// Clazz is always a class inheriting from Loggable
public Object newInstance(Class clazz) {
return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] {clazz}, handler);
}
private InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Call logStartingTime on object
// Call invoked method on object
// Call logEndingTime on object
return null;
}
};
}
私の抽象クラス:
public abstract class Loggable {
void logStartingTime() {
log.info(“start time = ” + new Date());
// also log some info about the state of the object
}
void logEndingTime() {
log.info(“ending time = ” + new Date());
// also log some info about the state of the object
}
}