PDA

View Full Version : unable to proxy using BeanNameAutoProxyCreator



muthiahmerchant
Jul 20th, 2007, 10:05 AM
I am trying to log the start, end and time taken to execute each method. I have the following in applicationContext.xml



<bean id="managerProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNa meAutoProxyCreator">
<property name="beanNames"><value>*Image*</value></property>
<property name="proxyTargetClass"><value>true</value></property>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
<value>loggingInterceptor</value>
</list>
</property>
</bean>

<bean id="myInterceptor" class="com.company.interceptor.MyInterceptor" />
<bean id="loggingInterceptor" class="org.springframework.aop.interceptor.SimpleTraceInt erceptor"/>

<bean id="hibImage" class="com.company.grayline.image.HibImageImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>


MyInterceptor code looks like this



import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MyInterceptor implements MethodInterceptor {
private final Log logger = LogFactory.getLog("MAIN");

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

if(logger.isInfoEnabled()){
logger.info("********Beginning method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
}
long startTime = System.currentTimeMillis();
try {
Object retVal = methodInvocation.proceed();
return retVal;
} finally {
if(logger.isInfoEnabled()){
logger.info(">>>>>>>>>>>>>Method invocation time: " + (System.currentTimeMillis() - startTime) + " msecs.");
logger.info("##########Ending method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
}
}
}

}



it does not log anything. Can anyone point out what is that I am doing wrong.

Thanks

karldmoore
Jul 20th, 2007, 12:21 PM
Is it possible to see the rest of the code you are using to run this?

muthiahmerchant
Jul 20th, 2007, 03:22 PM
I am not sure what other code to provide. is there any thing specific that you want to take a look, like web.xml etc.

Jörg Heinicke
Jul 20th, 2007, 10:52 PM
What type of object is HibImageImpl for example to which the interceptors should be applied? How do you retrieve it from the application context? It's suspicious when Hibernate is involved as it seems since Hibernate creates its own proxies which also might wipe out your interceptors.

Jörg

muthiahmerchant
Jul 21st, 2007, 01:54 AM
this is my hibImageImpl code



package com.company.image;

import com.company.model.Images;
import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport;

public class HibImageImpl extends HibernateDaoSupport implements ImageIntf {
protected Logger log = Logger.getLogger("MAIN");
Images img;

public HibImageImpl() {
}

public List<Images> getHeaderImage() {
List l = getHibernateTemplate().findByNamedQuery("header.image");
log.info("retrieved images from db. size of resultset = " + l.size());
List<Images> imgList = new ArrayList<Images>();
for (int i = 0; i < l.size(); i++) {
Object[] row = (Object[])l.get(i);
img = new Images();
img.setImagePath((String)row[0]);
img.setDescription((String)row[1]);

imgList.add(img);
}
return imgList;
}

public void setImg(Images img) {
this.img = img;
}
}


Thanks for all the help