SHARE
TWEET

Untitled

a guest Jun 18th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.example.version2.annotation.Timing;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.config.BeanPostProcessor;
  4. import org.springframework.stereotype.Component;
  5. import java.lang.reflect.InvocationHandler;
  6. import java.lang.reflect.Method;
  7. import java.lang.reflect.Proxy;
  8.  
  9. @Component
  10. public class TimingBeanPostProcessor implements BeanPostProcessor {
  11.  
  12.     @Override
  13.     public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
  14.         Class type = bean.getClass();
  15.         Method[] methods = type.getMethods();
  16.         for (Method method : methods) {
  17.             if (method.isAnnotationPresent(Timing.class)) {
  18.                 Object proxy = Proxy.newProxyInstance(type.getClassLoader(),type.getInterfaces(), new InvocationHandler() {
  19.                     @Override
  20.                     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  21.                         long before = System.nanoTime();
  22.                         Object retVal = method.invoke(bean, args);
  23.                         long after = System.nanoTime();
  24.                         System.out.println("Method worked: " + (after - before) + " nano seconds");
  25.                         return retVal;
  26.                     }
  27.                 });
  28.                 return proxy;
  29.             } else {
  30.                 return bean;
  31.             }
  32.         }
  33.  
  34.          return bean;
  35.     }
  36.  
  37.     @Override
  38.     public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
  39.         return bean;
  40.     }
  41.  
  42. }
  43.      
  44. import java.lang.annotation.Retention;
  45. import java.lang.annotation.RetentionPolicy;
  46.  
  47. @Retention(RetentionPolicy.RUNTIME)
  48. public @interface Timing {
  49. }
  50.      
  51. @Timing
  52. public  List<Map<String, Object>> selectQuery() {
  53.     String selectQuery = prop.getMYSQL_SELECT();
  54.     return mysqlTemplate.queryForList(selectQuery);
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top