Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have managed to hack something together based on <a href="http://developers-blog.org/blog/default/2009/07/13/Spring-AOP-Example" rel="nofollow">this Spring AOP Example</a>.</p> <p>I am not yet up to speed with Spring 3 so I have implemented this using Spring 2.5 - I dare say there are more elegant ways of achieving what you want. I have implemented this using <strong>System.out</strong>'s for simplicity but these could easily be converted to log4j calls.</p> <p>Initially I create a map between the Spring's bean names and the string representation of the object (<strong>InitBean</strong>). This map is used inside the <em>MethodInterceptor</em> - I did try making the <em>MethodInterceptor</em> an <em>InitializingBean</em> but the <em>MethodInterceptor</em> stopped working for some reason.</p> <p>Performing an equals between the bean passed in via the <em>MethodInterceptor</em> and the other beans in the application context did not work. e.g. by using something like "<strong><em>ctx.getBeansOfType(GoBean.class)</em></strong>" inside the <em>MethodInterceptor</em>. I presume this is because the object passed in via the <em>MethodInvocation</em> was a <em>GoBean</em> whereas objects obtained from the application context at this point are proxied (e.g. something like <strong><em>example.GoBean$$EnhancerByCGLIB$$bd27d40e</em></strong>).</p> <p>This is why I had to resort to a comparison of object string representations (which is not ideal). Also I specifically do not want to activate the <em>MethodInterceptor</em> logic when calling the "<strong><em>toString</em></strong>" method on an object (as since I'm using toString elsewhere leads to infinite loops and StackOverflow).</p> <p>I hope this is useful,</p> <p><strong>applicationContext.xml</strong></p> <pre><code>&lt;beans&gt; &lt;bean name="initBean" class="example.InitBean"/&gt; &lt;bean name="methodLoggingInterceptor" class="example.MethodLoggingInterceptor"&gt; &lt;property name="initBean" ref="initBean"/&gt; &lt;/bean&gt; &lt;bean name="proxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt; &lt;property name="beanNames"&gt; &lt;list&gt; &lt;value&gt;go*&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="interceptorNames"&gt; &lt;list&gt; &lt;value&gt;methodLoggingInterceptor&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean name="goBean1" class="example.GoBean" /&gt; &lt;bean name="goBean2" class="example.GoBean" /&gt; &lt;bean name="goBean3" class="example.GoBean" /&gt; &lt;/beans&gt; </code></pre> <p><strong>GoBean.java</strong></p> <pre><code>public class GoBean { public void execute(){ System.out.println(new Date()); } } </code></pre> <p><strong>SimpleTestClass.java</strong></p> <pre><code>public static void main( String[] args ){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ArrayList&lt;GoBean&gt; goBeans = new ArrayList&lt;GoBean&gt;(); goBeans.add((GoBean) ctx.getBean("goBean1")); goBeans.add((GoBean) ctx.getBean("goBean2")); goBeans.add((GoBean) ctx.getBean("goBean3")); for(GoBean g: goBeans){ g.execute(); } } </code></pre> <p><strong>InitBean.java</strong></p> <pre><code>public class InitBean implements ApplicationContextAware, InitializingBean { private ApplicationContext ctx; private Map&lt;String, String&gt; beanMap = new HashMap&lt;String,String&gt;(); 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&lt;String,String&gt; getBeanMap(){ return beanMap; } } </code></pre> <p><strong>MethodLoggingInterceptor.java</strong></p> <pre><code>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&lt;String,String&gt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload