SimpleBean.java
package roseindia.net;
public class SimpleBean {
public void sayHi() {
System.out.println("Hi");
}
public void doOtherWork() {
System.out.println("Doing Other Work");
}
}
ProfileInterceptorClass.java
package roseindia.net;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;
public class ProfileInterceptorClass implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
// TODO Auto-generated method stub
StopWatch stopWatch = new StopWatch();
stopWatch.start(methodInvocation.getMethod().getName());
Object object = methodInvocation.proceed();
stopWatch.stop();
timePeriod(methodInvocation, stopWatch.getTotalTimeMillis());
return object;
}
private void timePeriod(MethodInvocation methodInvocation, long time) {
String methodName = methodInvocation.getMethod().getName();
System.out.println(methodName + "took time" + time);
}
}
MainClaz.java
package roseindia.net;
import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.ProxyFactory;
public class MainClaz {
public static void main(String[] args) {
SimpleBean simpleBean = getSimpleBean();
simpleBean.sayHi();
simpleBean.doOtherWork();
}
private static SimpleBean getSimpleBean() {
SimpleBean simpleBean = new SimpleBean();
ProxyFactory proxyFactory = new ProxyFactory();
Advice advice = new ProfileInterceptorClass();
proxyFactory.setTarget(simpleBean);
SimpleBean proxy = (SimpleBean) proxyFactory.getProxy();
return proxy;
}
}
| Hi Doing Other Work |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.