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