001/*
002 * Copyright 2002-2016 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 * Indicates that a bean should be given preference when multiple candidates
027 * are qualified to autowire a single-valued dependency. If exactly one
028 * 'primary' bean exists among the candidates, it will be the autowired value.
029 *
030 * <p>This annotation is semantically equivalent to the {@code <bean>} element's
031 * {@code primary} attribute in Spring XML.
032 *
033 * <p>May be used on any class directly or indirectly annotated with
034 * {@code @Component} or on methods annotated with @{@link Bean}.
035 *
036 * <h2>Example</h2>
037 * <pre class="code">
038 * &#064;Component
039 * public class FooService {
040 *
041 *     private FooRepository fooRepository;
042 *
043 *     &#064;Autowired
044 *     public FooService(FooRepository fooRepository) {
045 *         this.fooRepository = fooRepository;
046 *     }
047 * }
048 *
049 * &#064;Component
050 * public class JdbcFooRepository extends FooRepository {
051 *
052 *     public JdbcFooRepository(DataSource dataSource) {
053 *         // ...
054 *     }
055 * }
056 *
057 * &#064;Primary
058 * &#064;Component
059 * public class HibernateFooRepository extends FooRepository {
060 *
061 *     public HibernateFooRepository(SessionFactory sessionFactory) {
062 *         // ...
063 *     }
064 * }
065 * </pre>
066 *
067 * <p>Because {@code HibernateFooRepository} is marked with {@code @Primary},
068 * it will be injected preferentially over the jdbc-based variant assuming both
069 * are present as beans within the same Spring application context, which is
070 * often the case when component-scanning is applied liberally.
071 *
072 * <p>Note that using {@code @Primary} at the class level has no effect unless
073 * component-scanning is being used. If a {@code @Primary}-annotated class is
074 * declared via XML, {@code @Primary} annotation metadata is ignored, and
075 * {@code <bean primary="true|false"/>} is respected instead.
076 *
077 * @author Chris Beams
078 * @author Juergen Hoeller
079 * @since 3.0
080 * @see Lazy
081 * @see Bean
082 * @see ComponentScan
083 * @see org.springframework.stereotype.Component
084 */
085@Target({ElementType.TYPE, ElementType.METHOD})
086@Retention(RetentionPolicy.RUNTIME)
087@Documented
088public @interface Primary {
089
090}