|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.annotation; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.BeanFactoryUtils; |
| 22 | +import org.springframework.beans.factory.FactoryBean; |
| 23 | +import org.springframework.context.ApplicationContext; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Andy Wilkinson |
| 27 | + */ |
| 28 | +public class AggressiveFactoryBeanInstantiationTests { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void directlyRegisteredFactoryBean() { |
| 32 | + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { |
| 33 | + context.register(SimpleFactoryBean.class); |
| 34 | + context.addBeanFactoryPostProcessor((factory) -> { |
| 35 | + BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, String.class); |
| 36 | + }); |
| 37 | + context.refresh(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void beanMethodFactoryBean() { |
| 43 | + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { |
| 44 | + context.register(BeanMethodConfiguration.class); |
| 45 | + context.addBeanFactoryPostProcessor((factory) -> { |
| 46 | + BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, String.class); |
| 47 | + }); |
| 48 | + context.refresh(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + @Configuration |
| 54 | + static class BeanMethodConfiguration { |
| 55 | + |
| 56 | + @Bean |
| 57 | + public SimpleFactoryBean simpleFactoryBean(ApplicationContext applicationContext) { |
| 58 | + return new SimpleFactoryBean(applicationContext); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + static class SimpleFactoryBean implements FactoryBean<Object> { |
| 64 | + |
| 65 | + public SimpleFactoryBean(ApplicationContext applicationContext) { |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Object getObject() { |
| 70 | + return new Object(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Class<?> getObjectType() { |
| 75 | + return Object.class; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments