001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.context.annotation;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024
025/**
026 * Enables support for handling components marked with AspectJ's {@code @Aspect} annotation,
027 * similar to functionality found in Spring's {@code <aop:aspectj-autoproxy>} XML element.
028 * To be used on @{@link Configuration} classes as follows:
029 *
030 * <pre class="code">
031 * &#064;Configuration
032 * &#064;EnableAspectJAutoProxy
033 * public class AppConfig {
034 *
035 *     &#064;Bean
036 *     public FooService fooService() {
037 *         return new FooService();
038 *     }
039 *
040 *     &#064;Bean
041 *     public MyAspect myAspect() {
042 *         return new MyAspect();
043 *     }
044 * }</pre>
045 *
046 * Where {@code FooService} is a typical POJO component and {@code MyAspect} is an
047 * {@code @Aspect}-style aspect:
048 *
049 * <pre class="code">
050 * public class FooService {
051 *
052 *     // various methods
053 * }</pre>
054 *
055 * <pre class="code">
056 * &#064;Aspect
057 * public class MyAspect {
058 *
059 *     &#064;Before("execution(* FooService+.*(..))")
060 *     public void advice() {
061 *         // advise FooService methods as appropriate
062 *     }
063 * }</pre>
064 *
065 * In the scenario above, {@code @EnableAspectJAutoProxy} ensures that {@code MyAspect}
066 * will be properly processed and that {@code FooService} will be proxied mixing in the
067 * advice that it contributes.
068 *
069 * <p>Users can control the type of proxy that gets created for {@code FooService} using
070 * the {@link #proxyTargetClass()} attribute. The following enables CGLIB-style 'subclass'
071 * proxies as opposed to the default interface-based JDK proxy approach.
072 *
073 * <pre class="code">
074 * &#064;Configuration
075 * &#064;EnableAspectJAutoProxy(proxyTargetClass=true)
076 * public class AppConfig {
077 *     // ...
078 * }</pre>
079 *
080 * <p>Note that {@code @Aspect} beans may be component-scanned like any other.
081 * Simply mark the aspect with both {@code @Aspect} and {@code @Component}:
082 *
083 * <pre class="code">
084 * package com.foo;
085 *
086 * &#064;Component
087 * public class FooService { ... }
088 *
089 * &#064;Aspect
090 * &#064;Component
091 * public class MyAspect { ... }</pre>
092 *
093 * Then use the @{@link ComponentScan} annotation to pick both up:
094 *
095 * <pre class="code">
096 * &#064;Configuration
097 * &#064;ComponentScan("com.foo")
098 * &#064;EnableAspectJAutoProxy
099 * public class AppConfig {
100 *
101 *     // no explicit &#064Bean definitions required
102 * }</pre>
103 *
104 * <b>Note: {@code @EnableAspectJAutoProxy} applies to its local application context only,
105 * allowing for selective proxying of beans at different levels.</b> Please redeclare
106 * {@code @EnableAspectJAutoProxy} in each individual context, e.g. the common root web
107 * application context and any separate {@code DispatcherServlet} application contexts,
108 * if you need to apply its behavior at multiple levels.
109 *
110 * <p>This feature requires the presence of {@code aspectjweaver} on the classpath.
111 * While that dependency is optional for {@code spring-aop} in general, it is required
112 * for {@code @EnableAspectJAutoProxy} and its underlying facilities.
113 *
114 * @author Chris Beams
115 * @author Juergen Hoeller
116 * @since 3.1
117 * @see org.aspectj.lang.annotation.Aspect
118 */
119@Target(ElementType.TYPE)
120@Retention(RetentionPolicy.RUNTIME)
121@Documented
122@Import(AspectJAutoProxyRegistrar.class)
123public @interface EnableAspectJAutoProxy {
124
125        /**
126         * Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
127         * to standard Java interface-based proxies. The default is {@code false}.
128         */
129        boolean proxyTargetClass() default false;
130
131        /**
132         * Indicate that the proxy should be exposed by the AOP framework as a {@code ThreadLocal}
133         * for retrieval via the {@link org.springframework.aop.framework.AopContext} class.
134         * Off by default, i.e. no guarantees that {@code AopContext} access will work.
135         * @since 4.3.1
136         */
137        boolean exposeProxy() default false;
138
139}