Using AspectJ, I can get hold of the singleton instance of an aspect using the aspectOf() method:
MyAspect aspect = Aspects.aspectOf(MyAspect.class);
// now perform post-application startup initialisation, eg:
aspect.setLogService(logging);
Is there a way to get a list of all of the aspects in my running Java application? I’m using AspectJ 1.6.7 with load-time weaving, but can upgrade versions if required.
A list of all of the aspects in my application will allow me to inject dependencies into them at startup.
A rather hackish solution, but a solution nonetheless: manually load the
aop.xmlfile from the classpath and parse it to get the aspect classnames:Then aspect instances can be fetched like so: