Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.example.version2.annotation.Timing;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanPostProcessor;
- import org.springframework.stereotype.Component;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- @Component
- public class TimingBeanPostProcessor implements BeanPostProcessor {
- @Override
- public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
- Class type = bean.getClass();
- Method[] methods = type.getMethods();
- for (Method method : methods) {
- if (method.isAnnotationPresent(Timing.class)) {
- Object proxy = Proxy.newProxyInstance(type.getClassLoader(),type.getInterfaces(), new InvocationHandler() {
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- long before = System.nanoTime();
- Object retVal = method.invoke(bean, args);
- long after = System.nanoTime();
- System.out.println("Method worked: " + (after - before) + " nano seconds");
- return retVal;
- }
- });
- return proxy;
- } else {
- return bean;
- }
- }
- return bean;
- }
- @Override
- public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
- return bean;
- }
- }
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- @Retention(RetentionPolicy.RUNTIME)
- public @interface Timing {
- }
- @Timing
- public List<Map<String, Object>> selectQuery() {
- String selectQuery = prop.getMYSQL_SELECT();
- return mysqlTemplate.queryForList(selectQuery);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement