I managed to hack something based on this Spring AOP example .
I am not up to speed with Spring 3 yet, so I implemented it with Spring 2.5 - I dare say that there are more elegant ways to achieve what you want. I implemented this with System.out for simplicity, but they could easily be converted to log4j calls.
First, I create a map between the Spring bean names and the string representation of the object ( InitBean ). This map is used inside the MethodInterceptor - I tried to make the MethodInterceptor the initialization bean, but the MethodInterceptor stopped working for some reason.
Performing equality between the bean passed through the MethodInterceptor and the other beans in the application context does not work. for example using something like " ctx.getBeansOfType (GoBean.class) " inside a MethodInterceptor. I assume this is because the object passed through MethodInvocation was GoBean, while the objects obtained from the application context at this point are proxied (for example, something like example.GoBean $$ EnhancerByCGLIB $$ bd27d40e ).
That's why I had to resort to comparing representations of string objects (which is not ideal). In addition, I specifically do not want to activate the MethodInterceptor logic when calling the " toString " method on an object (since, as I use toString elsewhere, this leads to infinite loops and StackOverflow).
Hope this is helpful
applicationContext.xml
<beans> <bean name="initBean" class="example.InitBean"/> <bean name="methodLoggingInterceptor" class="example.MethodLoggingInterceptor"> <property name="initBean" ref="initBean"/> </bean> <bean name="proxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>go*</value> </list> </property> <property name="interceptorNames"> <list> <value>methodLoggingInterceptor</value> </list> </property> </bean> <bean name="goBean1" class="example.GoBean" /> <bean name="goBean2" class="example.GoBean" /> <bean name="goBean3" class="example.GoBean" /> </beans>
Gobean.java
public class GoBean { public void execute(){ System.out.println(new Date()); } }
SimpleTestClass.java
public static void main( String[] args ){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ArrayList<GoBean> goBeans = new ArrayList<GoBean>(); goBeans.add((GoBean) ctx.getBean("goBean1")); goBeans.add((GoBean) ctx.getBean("goBean2")); goBeans.add((GoBean) ctx.getBean("goBean3")); for(GoBean g: goBeans){ g.execute(); } }
InitBean.java
public class InitBean implements ApplicationContextAware, InitializingBean { private ApplicationContext ctx; private Map<String, String> beanMap = new HashMap<String,String>(); public void setApplicationContext(ApplicationContext ac) throws BeansException { ctx = ac; } public void afterPropertiesSet() throws Exception { for(String beanName: ctx.getBeanNamesForType(GoBean.class)){ beanMap.put(ctx.getBean(beanName).toString(), beanName); } } public Map<String,String> getBeanMap(){ return beanMap; } }
MethodLoggingInterceptor.java
public class MethodLoggingInterceptor implements MethodInterceptor{ private InitBean initBean; public Object invoke(MethodInvocation method) throws Throwable { if (!"toString".equals(method.getMethod().getName())) { StringBuilder sb = new StringBuilder(); Object obj = method.getThis(); if (obj instanceof GoBean) { Map<String,String> beanMap = initBean.getBeanMap(); String objToString = obj.toString(); if (beanMap.containsKey(objToString)) { System.out.println(beanMap.get(objToString)); sb.append("bean: "); sb.append(beanMap.get(objToString)); sb.append(" : "); } } sb.append(method.getMethod().getDeclaringClass()); sb.append('.'); sb.append(method.getMethod().getName()); System.out.println(sb.toString() + " starts"); Object result = method.proceed(); System.out.println(sb.toString() + " finished"); return result; } else { return method.proceed(); } } public void setInitBean(InitBean ib) { this.initBean = ib; } }