Is possible to get the proxy of a given object in Spring? I need to call a function of a subclass. But, obviously, when I do a direct call, the aspects aren't applied. Here's an example:
public class Parent { public doSomething() { Parent proxyOfMe = Spring.getProxyOfMe(this); // (please) Method method = this.class.getMethod("sayHello"); method.invoke(proxyOfMe); } } public class Child extends Parent { @Secured("president") public void sayHello() { System.out.println("Hello Mr. President"); } } I've found a way of achieving this. It works, but I think is not very elegant:
public class Parent implements BeanNameAware { @Autowired private ApplicationContext applicationContext; private String beanName; // Getter public doSomething() { Parent proxyOfMe = applicationContext.getBean(beanName, Parent.class); Method method = this.class.getMethod("sayHello"); method.invoke(proxyOfMe); } }