|
| 1 | +/* |
| 2 | + * Copyright 2002-2011 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.annotation; |
| 18 | + |
| 19 | +import java.lang.annotation.Documented; |
| 20 | +import java.lang.annotation.ElementType; |
| 21 | +import java.lang.annotation.Retention; |
| 22 | +import java.lang.annotation.RetentionPolicy; |
| 23 | +import java.lang.annotation.Target; |
| 24 | + |
| 25 | +import org.springframework.context.weaving.DefaultContextLoadTimeWeaver; |
| 26 | +import org.springframework.instrument.classloading.LoadTimeWeaver; |
| 27 | + |
| 28 | +/** |
| 29 | + * Activates a Spring {@link LoadTimeWeaver} for this application context, available as |
| 30 | + * a bean with the name "loadTimeWeaver", similar to the {@code <context:load-time-weaver>} |
| 31 | + * element in Spring XML. |
| 32 | + * To be used |
| 33 | + * on @{@link org.springframework.context.annotation.Configuration Configuration} classes; |
| 34 | + * the simplest possible example of which follows: |
| 35 | + * <pre class="code"> |
| 36 | + * @Configuration |
| 37 | + * @EnableLoadTimeWeaving |
| 38 | + * public class AppConfig { |
| 39 | + * // application-specific @Bean definitions ... |
| 40 | + * }</pre> |
| 41 | + * |
| 42 | + * The example above is equivalent to the following Spring XML configuration: |
| 43 | + * <pre class="code"> |
| 44 | + * {@code |
| 45 | + * <beans> |
| 46 | + * <context:load-time-weaver/> |
| 47 | + * <!-- application-specific <bean> definitions --> |
| 48 | + * </beans> |
| 49 | + * }</pre> |
| 50 | + * |
| 51 | + * <h2>The {@code LoadTimeWeaverAware} interface</h2> |
| 52 | + * Any bean that implements the {@link |
| 53 | + * org.springframework.context.weaving.LoadTimeWeaverAware LoadTimeWeaverAware} interface |
| 54 | + * will then receive the {@code LoadTimeWeaver} reference automatically; for example, |
| 55 | + * Spring's JPA bootstrap support. |
| 56 | + * |
| 57 | + * <h2>Customizing the {@code LoadTimeWeaver}</h2> |
| 58 | + * The default weaver is determined automatically. As of Spring 3.1: detecting |
| 59 | + * Sun's GlassFish, Oracle's OC4J, Spring's VM agent and any ClassLoader supported by |
| 60 | + * Spring's {@link |
| 61 | + * org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver |
| 62 | + * ReflectiveLoadTimeWeaver} (for example, the {@link |
| 63 | + * org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader |
| 64 | + * TomcatInstrumentableClassLoader}). |
| 65 | + * |
| 66 | + * <p>To customize the weaver used, the {@code @Configuration} class annotated with |
| 67 | + * {@code @EnableLoadTimeWeaving} may also implement the {@link LoadTimeWeaverConfigurer} |
| 68 | + * interface and return a custom {@code LoadTimeWeaver} instance through the |
| 69 | + * {@code #getLoadTimeWeaver} method: |
| 70 | + * <pre class="code"> |
| 71 | + * @Configuration |
| 72 | + * @EnableLoadTimeWeaving |
| 73 | + * public class AppConfig implements LoadTimeWeaverConfigurer { |
| 74 | + * @Override |
| 75 | + * public LoadTimeWeaver getLoadTimeWeaver() { |
| 76 | + * MyLoadTimeWeaver ltw = new MyLoadTimeWeaver(); |
| 77 | + * ltw.addClassTransformer(myClassFileTransformer); |
| 78 | + * // ... |
| 79 | + * return ltw; |
| 80 | + * } |
| 81 | + * }</pre> |
| 82 | + * |
| 83 | + * <p>The example above can be compared to the following Spring XML configuration: |
| 84 | + * <pre class="code"> |
| 85 | + * {@code |
| 86 | + * <beans> |
| 87 | + * <context:load-time-weaver weaverClass="com.acme.MyLoadTimeWeaver"/> |
| 88 | + * </beans> |
| 89 | + * }</pre> |
| 90 | + * |
| 91 | + * <p>The code example differs from the XML example in that it actually instantiates the |
| 92 | + * {@code MyLoadTimeWeaver} type, meaning that it can also configure the instance, e.g. |
| 93 | + * calling the {@code #addClassTransformer} method. This demonstrates how the code-based |
| 94 | + * configuration approach is more flexible through direct programmatic access. |
| 95 | + * |
| 96 | + * <h2>Enabling AspectJ-based weaving</h2> |
| 97 | + * AspectJ load-time weaving may be enabled with the {@link #aspectjWeaving()} |
| 98 | + * attribute, which will cause the {@linkplain |
| 99 | + * org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter AspectJ class transformer} to |
| 100 | + * be registered through {@link LoadTimeWeaver#addTransformer}. AspectJ weaving will be |
| 101 | + * activated by default if a "META-INF/aop.xml" resource is present on the classpath. |
| 102 | + * Example: |
| 103 | + * <pre class="code"> |
| 104 | + * @Configuration |
| 105 | + * @EnableLoadTimeWeaving(aspectjWeaving=ENABLED) |
| 106 | + * public class AppConfig { |
| 107 | + * }</pre> |
| 108 | + * |
| 109 | + * <p>The example above can be compared to the following Spring XML configuration: |
| 110 | + * <pre class="code"> |
| 111 | + * {@code |
| 112 | + * <beans> |
| 113 | + * <context:load-time-weaver aspectj-weaving="on"/> |
| 114 | + * </beans> |
| 115 | + * }</pre> |
| 116 | + * |
| 117 | + * <p>The two examples are equivalent with one significant exception: in the XML case, |
| 118 | + * the functionality of {@code <context:spring-configured>} is implicitly enabled when |
| 119 | + * {@code aspectj-weaving} is "on". This does not occur when using |
| 120 | + * {@code @EnableLoadTimeWeaving(aspectjWeaving=ENABLED)}, although this may change in |
| 121 | + * future revisions. |
| 122 | + * |
| 123 | + * @author Chris Beams |
| 124 | + * @since 3.1 |
| 125 | + * @see LoadTimeWeaver |
| 126 | + * @see DefaultContextLoadTimeWeaver |
| 127 | + * @see org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter |
| 128 | + */ |
| 129 | +@Target(ElementType.TYPE) |
| 130 | +@Retention(RetentionPolicy.RUNTIME) |
| 131 | +@Documented |
| 132 | +@Import(LoadTimeWeavingConfiguration.class) |
| 133 | +public @interface EnableLoadTimeWeaving { |
| 134 | + |
| 135 | + /** |
| 136 | + * Whether AspectJ weaving should be enabled. |
| 137 | + */ |
| 138 | + AspectJWeaving aspectjWeaving() default AspectJWeaving.AUTODETECT; |
| 139 | + |
| 140 | + public enum AspectJWeaving { |
| 141 | + |
| 142 | + /** |
| 143 | + * Switches on Spring-based AspectJ load-time weaving. |
| 144 | + */ |
| 145 | + ENABLED, |
| 146 | + |
| 147 | + /** |
| 148 | + * Switches off Spring-based AspectJ load-time weaving (even if a |
| 149 | + * "META-INF/aop.xml" resource is present on the classpath). |
| 150 | + */ |
| 151 | + DISABLED, |
| 152 | + |
| 153 | + /** |
| 154 | + * Switches on AspectJ load-time weaving if a "META-INF/aop.xml" resource |
| 155 | + * is present in the classpath. If there is no such resource, then AspectJ |
| 156 | + * load-time weaving will be switched off. |
| 157 | + */ |
| 158 | + AUTODETECT; |
| 159 | + } |
| 160 | +} |
0 commit comments