/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Principal;
import java.security.PrivilegedAction;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.security.auth.Subject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static org.junit.Assert.*;
import org.junit.Test;
import test.beans.DerivedTestBean;
import test.beans.DummyFactory;
import test.beans.ITestBean;
import test.beans.LifecycleBean;
import test.beans.NestedTestBean;
import test.beans.TestBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ConstructorDependenciesBean;
import org.springframework.beans.factory.xml.DependenciesBean;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.StopWatch;
/**
* Tests properties population and autowire behavior.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rick Evans
* @author Sam Brannen
* @author Chris Beams
*/
public final class DefaultListableBeanFactoryTests {
private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class);
@Test
public void testUnreferencedSingletonWasInstantiated() {
KnowsIfInstantiated.clearInstantiationRecord();
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName());
assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
lbf.preInstantiateSingletons();
assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}
@Test
public void testLazyInitialization() {
KnowsIfInstantiated.clearInstantiationRecord();
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName());
p.setProperty("x1.(lazy-init)", "true");
assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
lbf.preInstantiateSingletons();
assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
lbf.getBean("x1");
assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}
@Test
public void testFactoryBeanDidNotCreatePrototype() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("x1.(class)", DummyFactory.class.getName());
// Reset static state
DummyFactory.reset();
p.setProperty("x1.singleton", "false");
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
assertEquals(TestBean.class, lbf.getType("x1"));
lbf.preInstantiateSingletons();
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
lbf.getBean("x1");
assertEquals(TestBean.class, lbf.getType("x1"));
assertTrue(lbf.containsBean("x1"));
assertTrue(lbf.containsBean("&x1"));
assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated());
}
@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("x1.(class)", DummyFactory.class.getName());
// Reset static state
DummyFactory.reset();
p.setProperty("x1.(singleton)", "false");
p.setProperty("x1.singleton", "false");
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
assertEquals(0, beanNames.length);
assertFalse(lbf.containsSingleton("x1"));
assertTrue(lbf.containsBean("x1"));
assertTrue(lbf.containsBean("&x1"));
assertFalse(lbf.isSingleton("x1"));
assertFalse(lbf.isSingleton("&x1"));
assertTrue(lbf.isPrototype("x1"));
assertTrue(lbf.isPrototype("&x1"));
assertTrue(lbf.isTypeMatch("x1", TestBean.class));
assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
assertEquals(TestBean.class, lbf.getType("x1"));
assertEquals(DummyFactory.class, lbf.getType("&x1"));
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
@Test
public void testPrototypeSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("x1.(class)", DummyFactory.class.getName());
// Reset static state
DummyFactory.reset();
p.setProperty("x1.(singleton)", "false");
p.setProperty("x1.singleton", "true");
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
String[]
- 1
- 2
- 3
- 4
- 5
前往页