001/*
002 * Copyright 2002-2013 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 whether a bean is to be lazily initialized.
027 *
028 * <p>May be used on any class directly or indirectly annotated with {@link
029 * org.springframework.stereotype.Component @Component} or on methods annotated with
030 * {@link Bean @Bean}.
031 *
032 * <p>If this annotation is not present on a {@code @Component} or {@code @Bean} definition,
033 * eager initialization will occur. If present and set to {@code true}, the {@code @Bean} or
034 * {@code @Component} will not be initialized until referenced by another bean or explicitly
035 * retrieved from the enclosing {@link org.springframework.beans.factory.BeanFactory
036 * BeanFactory}. If present and set to {@code false}, the bean will be instantiated on
037 * startup by bean factories that perform eager initialization of singletons.
038 *
039 * <p>If Lazy is present on a {@link Configuration @Configuration} class, this
040 * indicates that all {@code @Bean} methods within that {@code @Configuration}
041 * should be lazily initialized. If {@code @Lazy} is present and false on a {@code @Bean}
042 * method within a {@code @Lazy}-annotated {@code @Configuration} class, this indicates
043 * overriding the 'default lazy' behavior and that the bean should be eagerly initialized.
044 *
045 * <p>In addition to its role for component initialization, this annotation may also be placed
046 * on injection points marked with {@link org.springframework.beans.factory.annotation.Autowired}
047 * or {@link javax.inject.Inject}: In that context, it leads to the creation of a
048 * lazy-resolution proxy for all affected dependencies, as an alternative to using
049 * {@link org.springframework.beans.factory.ObjectFactory} or {@link javax.inject.Provider}.
050 *
051 * @author Chris Beams
052 * @author Juergen Hoeller
053 * @since 3.0
054 * @see Primary
055 * @see Bean
056 * @see Configuration
057 * @see org.springframework.stereotype.Component
058 */
059@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
060@Retention(RetentionPolicy.RUNTIME)
061@Documented
062public @interface Lazy {
063
064        /**
065         * Whether lazy initialization should occur.
066         */
067        boolean value() default true;
068
069}