Spring Aspects - madhusudana30/AlternativeJPAForWebSphere GitHub Wiki
Aspects The aspects that you use in LTW have to be AspectJ aspects. They can be written in either the AspectJ language itself or you can write your aspects in the @AspectJ-style. The latter option is of course only an option if you are using Java 5+, but it does mean that your aspects are then both valid AspectJ and Spring AOP aspects. Furthermore, the compiled aspect classes need to be available on the classpath.
'META-INF/aop.xml' The AspectJ LTW infrastructure is configured using one or more 'META-INF/aop.xml' files, that are on the Java classpath (either directly, or more typically in jar files).
The structure and contents of this file is detailed in the main AspectJ reference documentation, and the interested reader is referred to that resource. (I appreciate that this section is brief, but the 'aop.xml' file is 100% AspectJ - there is no Spring-specific information or semantics that apply to it, and so there is no extra value that I can contribute either as a result), so rather than rehash the quite satisfactory section that the AspectJ developers wrote, I am just directing you there.)
Required libraries (JARS) At a minimum you will need the following libraries to use the Spring Framework's support for AspectJ LTW:
spring-aop.jar (version 2.5 or later, plus all mandatory dependencies)
aspectjweaver.jar (version 1.6.8 or later)
If you are using the Spring-provided agent to enable instrumentation, you will also need:
spring-instrument.jar
Spring configuration The key component in Spring's LTW support is the LoadTimeWeaver interface (in the org.springframework.instrument.classloading package), and the numerous implementations of it that ship with the Spring distribution. A LoadTimeWeaver is responsible for adding one or more java.lang.instrument.ClassFileTransformers to a ClassLoader at runtime, which opens the door to all manner of interesting applications, one of which happens to be the LTW of aspects.
[Tip] Tip If you are unfamiliar with the idea of runtime class file transformation, you are encouraged to read the Javadoc API documentation for the java.lang.instrument package before continuing. This is not a huge chore because there is - rather annoyingly - precious little documentation there... the key interfaces and classes will at least be laid out in front of you for reference as you read through this section.
Configuring a LoadTimeWeaver for a particular ApplicationContext can be as easy as adding one line. (Please note that you almost certainly will need to be using an ApplicationContext as your Spring container - typically a BeanFactory will not be enough because the LTW support makes use of BeanFactoryPostProcessors.)
To enable the Spring Framework's LTW support, you need to configure a LoadTimeWeaver, which typically is done using the @EnableLoadTimeWeaving annotation.
@Configuration @EnableLoadTimeWeaving public class AppConfig {
} Alternatively, if you prefer XML based configuration, use the context:load-time-weaver/ element. Note that the element is defined in the 'context' namespace.
<context:load-time-weaver/>
Table 9.1. DefaultContextLoadTimeWeaver LoadTimeWeavers
Runtime Environment LoadTimeWeaver implementation Running in BEA's Weblogic 10
WebLogicLoadTimeWeaver
Running in IBM WebSphere Application Server 7
WebSphereLoadTimeWeaver
Running in Oracle's OC4J
OC4JLoadTimeWeaver
Running in GlassFish
GlassFishLoadTimeWeaver
Running in JBoss AS
JBossLoadTimeWeaver
JVM started with Spring InstrumentationSavingAgent
(java -javaagent:path/to/spring-instrument.jar)
InstrumentationLoadTimeWeaver
Fallback, expecting the underlying ClassLoader to follow common conventions (e.g. applicable to TomcatInstrumentableClassLoader and Resin)
ReflectiveLoadTimeWeaver
Note that these are just the LoadTimeWeavers that are autodetected when using the DefaultContextLoadTimeWeaver: it is of course possible to specify exactly which LoadTimeWeaver implementation that you wish to use.
To specify a specific LoadTimeWeaver with Java configuration implement the LoadTimeWeavingConfigurer interface and override the getLoadTimeWeaver() method:
@Configuration @EnableLoadTimeWeaving public class AppConfig implements LoadTimeWeavingConfigurer { @Override public LoadTimeWeaver getLoadTimeWeaver() { return new ReflectiveLoadTimeWeaver(); } } If you are using XML based configuration you can specify the fully-qualified classname as the value of the 'weaver-class' attribute on the context:load-time-weaver/ element:
<context:load-time-weaver
weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
There is one final attribute of the configuration left to discuss: the 'aspectjWeaving' attribute (or 'aspectj-weaving' if you are using XML). This is a simple attribute that controls whether LTW is enabled or not, it is as simple as that. It accepts one of three possible values, summarized below, with the default value if the attribute is not present being 'autodetect'
Table 9.2. AspectJ weaving attribute values
Annotation Value XML Value Explanation ENABLED
on
AspectJ weaving is on, and aspects will be woven at load-time as appropriate.
DISABLED
off
LTW is off... no aspect will be woven at load-time.
AUTODETECT
autodetect
If the Spring LTW infrastructure can find at least one 'META-INF/aop.xml' file, then AspectJ weaving is on, else it is off. This is the default value.