001/*
002 * Copyright 2002-2012 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.beans.factory.config;
018
019import java.io.Serializable;
020import javax.inject.Provider;
021
022import org.springframework.beans.BeansException;
023import org.springframework.beans.factory.BeanFactory;
024import org.springframework.util.Assert;
025
026/**
027 * A {@link org.springframework.beans.factory.FactoryBean} implementation that
028 * returns a value which is a JSR-330 {@link javax.inject.Provider} that in turn
029 * returns a bean sourced from a {@link org.springframework.beans.factory.BeanFactory}.
030 *
031 * <p>This is basically a JSR-330 compliant variant of Spring's good old
032 * {@link ObjectFactoryCreatingFactoryBean}. It can be used for traditional
033 * external dependency injection configuration that targets a property or
034 * constructor argument of type {@code javax.inject.Provider}, as an
035 * alternative to JSR-330's {@code @Inject} annotation-driven approach.
036 *
037 * @author Juergen Hoeller
038 * @since 3.0.2
039 * @see javax.inject.Provider
040 * @see ObjectFactoryCreatingFactoryBean
041 */
042public class ProviderCreatingFactoryBean extends AbstractFactoryBean<Provider<Object>> {
043
044        private String targetBeanName;
045
046
047        /**
048         * Set the name of the target bean.
049         * <p>The target does not <i>have</i> to be a non-singleton bean, but realistically
050         * always will be (because if the target bean were a singleton, then said singleton
051         * bean could simply be injected straight into the dependent object, thus obviating
052         * the need for the extra level of indirection afforded by this factory approach).
053         */
054        public void setTargetBeanName(String targetBeanName) {
055                this.targetBeanName = targetBeanName;
056        }
057
058        @Override
059        public void afterPropertiesSet() throws Exception {
060                Assert.hasText(this.targetBeanName, "Property 'targetBeanName' is required");
061                super.afterPropertiesSet();
062        }
063
064
065        @Override
066        public Class<?> getObjectType() {
067                return Provider.class;
068        }
069
070        @Override
071        protected Provider<Object> createInstance() {
072                return new TargetBeanProvider(getBeanFactory(), this.targetBeanName);
073        }
074
075
076        /**
077         * Independent inner class - for serialization purposes.
078         */
079        @SuppressWarnings("serial")
080        private static class TargetBeanProvider implements Provider<Object>, Serializable {
081
082                private final BeanFactory beanFactory;
083
084                private final String targetBeanName;
085
086                public TargetBeanProvider(BeanFactory beanFactory, String targetBeanName) {
087                        this.beanFactory = beanFactory;
088                        this.targetBeanName = targetBeanName;
089                }
090
091                @Override
092                public Object get() throws BeansException {
093                        return this.beanFactory.getBean(this.targetBeanName);
094                }
095        }
096
097}