Top Description Inners Methods
org.springframework.context.annotation

public @Interface EnableLoadTimeWeaving

extends Annotation
Annotations
@Target:TYPE
@Retention:RUNTIME
@Documented
@Import:LoadTimeWeavingConfiguration
Imports
java.lang.annotation.Documented, .ElementType, .Retention, .RetentionPolicy, .Target, org.springframework.context.weaving.DefaultContextLoadTimeWeaver, org.springframework.instrument.classloading.LoadTimeWeaver

Activates a Spring LoadTimeWeaver for this application context, available as a bean with the name "loadTimeWeaver", similar to the <context:load-time-weaver> element in Spring XML.

To be used on @Configuration classes; the simplest possible example of which follows:

@Configuration
@EnableLoadTimeWeaving
public class AppConfig {

    // application-specific @Bean definitions ...
}
The example above is equivalent to the following Spring XML configuration:
<beans>

    <context:load-time-weaver/>

    <!-- application-specific <bean> definitions -->

</beans>

The LoadTimeWeaverAware interface

Any bean that implements the LoadTimeWeaverAware interface will then receive the LoadTimeWeaver reference automatically; for example, Spring's JPA bootstrap support.

Customizing the LoadTimeWeaver

The default weaver is determined automatically: see DefaultContextLoadTimeWeaver.

To customize the weaver used, the @Configuration class annotated with @EnableLoadTimeWeaving may also implement the LoadTimeWeavingConfigurer interface and return a custom LoadTimeWeaver instance through the #getLoadTimeWeaver method:

@Configuration
@EnableLoadTimeWeaving
public class AppConfig implements LoadTimeWeavingConfigurer {

    @Override
    public LoadTimeWeaver getLoadTimeWeaver() {
        MyLoadTimeWeaver ltw = new MyLoadTimeWeaver();
        ltw.addClassTransformer(myClassFileTransformer);
        // ...
        return ltw;
    }
}

The example above can be compared to the following Spring XML configuration:

<beans>

    <context:load-time-weaver weaverClass="com.acme.MyLoadTimeWeaver"/>

</beans>

The code example differs from the XML example in that it actually instantiates the MyLoadTimeWeaver type, meaning that it can also configure the instance, e.g. calling the #addClassTransformer method. This demonstrates how the code-based configuration approach is more flexible through direct programmatic access.

Enabling AspectJ-based weaving

AspectJ load-time weaving may be enabled with the aspectjWeaving() attribute, which will cause the AspectJ class transformer to be registered through LoadTimeWeaver#addTransformer. AspectJ weaving will be activated by default if a "META-INF/aop.xml" resource is present on the classpath. Example:
@Configuration
@EnableLoadTimeWeaving(aspectjWeaving=ENABLED)
public class AppConfig {
}

The example above can be compared to the following Spring XML configuration:

<beans>

    <context:load-time-weaver aspectj-weaving="on"/>

</beans>

The two examples are equivalent with one significant exception: in the XML case, the functionality of <context:spring-configured> is implicitly enabled when aspectj-weaving is "on". This does not occur when using @EnableLoadTimeWeaving(aspectjWeaving=ENABLED). Instead you must explicitly add @EnableSpringConfigured (included in the spring-aspects module)

Author
Chris Beams
Since
3.1
See Also
LoadTimeWeaver, DefaultContextLoadTimeWeaver, org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
EnableLoadTimeWeaving.AspectJWeaving

AspectJ weaving enablement options.

Method Summary

Modifier and TypeMethod and Description
public EnableLoadTimeWeaving.AspectJWeaving
aspectjWeaving()

Whether AspectJ weaving should be enabled.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

aspectjWeavingback to summary
public EnableLoadTimeWeaving.AspectJWeaving aspectjWeaving()

Whether AspectJ weaving should be enabled.

org.springframework.context.annotation back to summary

public final Enum EnableLoadTimeWeaving.AspectJWeaving

extends Enum<EnableLoadTimeWeaving.AspectJWeaving>
Class Inheritance

AspectJ weaving enablement options.

Field Summary

Modifier and TypeField and Description
public static final EnableLoadTimeWeaving.AspectJWeaving
AUTODETECT

Switches on AspectJ load-time weaving if a "META-INF/aop.xml" resource is present in the classpath.

public static final EnableLoadTimeWeaving.AspectJWeaving
DISABLED

Switches off Spring-based AspectJ load-time weaving (even if a "META-INF/aop.xml" resource is present on the classpath).

public static final EnableLoadTimeWeaving.AspectJWeaving
ENABLED

Switches on Spring-based AspectJ load-time weaving.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static EnableLoadTimeWeaving.AspectJWeaving
public static EnableLoadTimeWeaving.AspectJWeaving[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

AUTODETECTback to summary
public static final EnableLoadTimeWeaving.AspectJWeaving AUTODETECT

Switches on AspectJ load-time weaving if a "META-INF/aop.xml" resource is present in the classpath. If there is no such resource, then AspectJ load-time weaving will be switched off.

DISABLEDback to summary
public static final EnableLoadTimeWeaving.AspectJWeaving DISABLED

Switches off Spring-based AspectJ load-time weaving (even if a "META-INF/aop.xml" resource is present on the classpath).

ENABLEDback to summary
public static final EnableLoadTimeWeaving.AspectJWeaving ENABLED

Switches on Spring-based AspectJ load-time weaving.

Constructor Detail

AspectJWeavingback to summary
private AspectJWeaving()

Method Detail

valueOfback to summary
public static EnableLoadTimeWeaving.AspectJWeaving valueOf(String name)
valuesback to summary
public static EnableLoadTimeWeaving.AspectJWeaving[] values()