I use Spring to configure aspects, and it's been working really well for me
over the last few weeks. Here are some extracts for how to get it to work:
Here's a simple beans.xml file, the only feature to note here is the use of
the "factory-method' attribute to specify the aspectOf method, otherwise it
looks and works just like any other Spring bean. (Note you need Spring v
1.1 for this to work - it will be out soon, I'm working from CVS HEAD in the
meantime).
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="myAspect"
class="org.xyz.MyAspect"
factory-method="aspectOf">
<property
name="enabled">
<value>true</true>
</property>
</bean>
</beans>
The aspect:
public aspect MyAspect {
private boolean isEnabled = true;
/** called by Spring */
public void setEnabled(boolean enabled) { this.isEnabled = enabled;
}
pointcut enabled() : if(isEnabled);
before() : somePointcutExpression() && enabled() {
// do something
}
}
Depending on the context in which you're using Spring, that might be it. If
you're writing a standalone application, you need to fire up the Spring
container yourself in the main method:
import org.springframework.beans.factory.xml.XmlBeanFactory;
import
org.springframework.core.io.FileSystemResource;
import
org.springframework.core.io.Resource;
...
Resource configFile = new
FileSystemResource("config/beans.xml");
XmlBeanFactory factory =
new
XmlBeanFactory(configFile);
factory.preInstantiateSingletons();
You can do a lot more than just enable/disable aspects using this mechanism
- I've used Spring to wire up aspects with bean references, property lists,
configuration data and all sorts.