001/*
002 * Copyright 2002-2017 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
025import org.springframework.core.env.AbstractEnvironment;
026import org.springframework.core.env.ConfigurableEnvironment;
027
028/**
029 * Indicates that a component is eligible for registration when one or more
030 * {@linkplain #value specified profiles} are active.
031 *
032 * <p>A <em>profile</em> is a named logical grouping that may be activated
033 * programmatically via {@link ConfigurableEnvironment#setActiveProfiles} or declaratively
034 * by setting the {@link AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
035 * spring.profiles.active} property as a JVM system property, as an
036 * environment variable, or as a Servlet context parameter in {@code web.xml}
037 * for web applications. Profiles may also be activated declaratively in
038 * integration tests via the {@code @ActiveProfiles} annotation.
039 *
040 * <p>The {@code @Profile} annotation may be used in any of the following ways:
041 * <ul>
042 * <li>as a type-level annotation on any class directly or indirectly annotated with
043 * {@code @Component}, including {@link Configuration @Configuration} classes</li>
044 * <li>as a meta-annotation, for the purpose of composing custom stereotype annotations</li>
045 * <li>as a method-level annotation on any {@link Bean @Bean} method</li>
046 * </ul>
047 *
048 * <p>If a {@code @Configuration} class is marked with {@code @Profile}, all of the
049 * {@code @Bean} methods and {@link Import @Import} annotations associated with that class
050 * will be bypassed unless one or more of the specified profiles are active. This is
051 * analogous to the behavior in Spring XML: if the {@code profile} attribute of the
052 * {@code beans} element is supplied e.g., {@code <beans profile="p1,p2">}, the
053 * {@code beans} element will not be parsed unless at least profile 'p1' or 'p2' has been
054 * activated. Likewise, if a {@code @Component} or {@code @Configuration} class is marked
055 * with {@code @Profile({"p1", "p2"})}, that class will not be registered or processed unless
056 * at least profile 'p1' or 'p2' has been activated.
057 *
058 * <p>If a given profile is prefixed with the NOT operator ({@code !}), the annotated
059 * component will be registered if the profile is <em>not</em> active &mdash; for example,
060 * given {@code @Profile({"p1", "!p2"})}, registration will occur if profile 'p1' is active
061 * or if profile 'p2' is <em>not</em> active.
062 *
063 * <p>If the {@code @Profile} annotation is omitted, registration will occur regardless
064 * of which (if any) profiles are active.
065 *
066 * <p><b>NOTE:</b> With {@code @Profile} on {@code @Bean} methods, a special scenario may
067 * apply: In the case of overloaded {@code @Bean} methods of the same Java method name
068 * (analogous to constructor overloading), an {@code @Profile} condition needs to be
069 * consistently declared on all overloaded methods. If the conditions are inconsistent,
070 * only the condition on the first declaration among the overloaded methods will matter.
071 * {@code @Profile} can therefore not be used to select an overloaded method with a
072 * particular argument signature over another; resolution between all factory methods
073 * for the same bean follows Spring's constructor resolution algorithm at creation time.
074 * <b>Use distinct Java method names pointing to the same {@link Bean#name bean name}
075 * if you'd like to define alternative beans with different profile conditions</b>;
076 * see {@code ProfileDatabaseConfig} in {@link Configuration @Configuration}'s javadoc.
077 *
078 * <p>When defining Spring beans via XML, the {@code "profile"} attribute of the
079 * {@code <beans>} element may be used. See the documentation in the
080 * {@code spring-beans} XSD (version 3.1 or greater) for details.
081 *
082 * @author Chris Beams
083 * @author Phillip Webb
084 * @author Sam Brannen
085 * @since 3.1
086 * @see ConfigurableEnvironment#setActiveProfiles
087 * @see ConfigurableEnvironment#setDefaultProfiles
088 * @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
089 * @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME
090 * @see Conditional
091 * @see org.springframework.test.context.ActiveProfiles
092 */
093@Target({ElementType.TYPE, ElementType.METHOD})
094@Retention(RetentionPolicy.RUNTIME)
095@Documented
096@Conditional(ProfileCondition.class)
097public @interface Profile {
098
099        /**
100         * The set of profiles for which the annotated component should be registered.
101         */
102        String[] value();
103
104}