I am trying to prototype some aop logic to handle logging logic. While I am doing it, I thought I would be nice to use BeanFactoryPostProcessor (not BeanPostProcessor) to automatically set bean name to 'name' property of target beans, so I can use it for logging if needed.
I got the aop autoproxying logic (aop:config) working before putting my BeanFactoryPostProcessor bean into the spring xml. Once BeanFactoryPostProcessor is there, aop autoproxying stops working (no more logging from aop adviser class). I know I can use BeanNameAware instead, but I would think BeanFactoryPostProcessor can work with aop:config, so I must have made a mistake somewhere.
Here are the config and codes (w/ spring 2.0)
Bean classes:
public class BeanObjects
{
private static final Log mLog = LogFactory.getLog(BeanObjects.class);
/** bean with a simple name property */
public static class SampleBean
{
private String name;
@PostConstruct
public void init()
{
mLog.info("init(" + name + ")");
}
public String getName()
{
return name;
}
public <T> T returnObject(T object)
{
return object;
}
public void throwException(Throwable e) throws Throwable
{
throw e;
}
public void setName(String name)
{
this.name = name;
}
@PreDestroy
public void destory()
{
mLog.info("destory(" + name + ")");
}
public String toString()
{
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append(name)
.toString();
}
}
/** instantiate to register sample bean's name */
public static class SampleBeanPostBeanFactory implements BeanFactoryPostProcessor
{
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
{
Map<String, SampleBean> beans = beanFactory.getBeansOfType(SampleBean.class);
for (String beanName : beans.keySet()) {
SampleBean sampleBean = beans.get(beanName);
sampleBean.setName(beanName);
}
}
}
}
public class AopTest
{
private static final Log mLog = LogFactory.getLog(AopTest.class);
private ListableBeanFactory mBeanFactory;
@Before
public void setUp()
{
mBeanFactory = new ClassPathXmlApplicationContext("/spring/aop/aop.xml");
}
@Test
public void testAopAdvice()
{
BeanObjects.SampleBean sample = (SampleBean) mBeanFactory.getBean("sampleBean_pointCut_before");
sample.getClass();
sample.returnObject("A");
sample.setName("name");
sample.getName();
try {
sample.throwException(new Exception("exception"));
} catch (Throwable e) {
}
}
}
config xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="sampleBean_pointCut_before"
class="spring.beans.BeanObjects$SampleBean" />
<bean id="sampleBean_pointCut_afterReturning"
class="spring.beans.BeanObjects$SampleBean" />
<bean id="sampleBean_pointCut_afterThrowing"
class="spring.beans.BeanObjects$SampleBean" />
<bean id="sampleBean_pointCut_around"
class="spring.beans.BeanObjects$SampleBean" />
<bean class="spring.beans.BeanObjects$SampleBeanPostBeanFactory" />
<bean id="methodLogger" class="spring.aop.LogAdvices$LogAdvice" />
/>