📄 defaultlistablebeanfactorytestsuite.java
字号:
}
public void testAutowireExistingBeanByNameWithDependencyCheck() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, new MutablePropertyValues());
lbf.registerBeanDefinition("spous", bd);
DependenciesBean existingBean = new DependenciesBean();
try {
lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
fail("Should have thrown UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException ex) {
// expected
}
}
public void testAutowireExistingBeanByNameWithNoDependencyCheck() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, new MutablePropertyValues());
lbf.registerBeanDefinition("spous", bd);
DependenciesBean existingBean = new DependenciesBean();
lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
assertNull(existingBean.getSpouse());
}
public void testAutowireExistingBeanByType() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, new MutablePropertyValues());
lbf.registerBeanDefinition("test", bd);
DependenciesBean existingBean = new DependenciesBean();
lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
TestBean test = (TestBean) lbf.getBean("test");
assertEquals(existingBean.getSpouse(), test);
}
public void testAutowireExistingBeanByTypeWithDependencyCheck() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
DependenciesBean existingBean = new DependenciesBean();
try {
lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
fail("Should have thrown UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException ex) {
// expected
}
}
public void testAutowireExistingBeanByTypeWithNoDependencyCheck() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
DependenciesBean existingBean = new DependenciesBean();
lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
assertNull(existingBean.getSpouse());
}
public void testInvalidAutowireMode() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
try {
lbf.autowireBeanProperties(new TestBean(), 0, false);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
}
public void testAutowireWithNoDependencies() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, new MutablePropertyValues());
lbf.registerBeanDefinition("rod", bd);
assertEquals(1, lbf.getBeanDefinitionCount());
Object registered = lbf.autowire(
NoDependencies.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
assertEquals(1, lbf.getBeanDefinitionCount());
assertTrue(registered instanceof NoDependencies);
}
public void testAutowireWithSatisfiedJavaBeanDependency() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
lbf.registerBeanDefinition("rod", bd);
assertEquals(1, lbf.getBeanDefinitionCount());
String name = "kerry";
// Depends on age, name and spouse (TestBean)
Object registered = lbf.autowire(
DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);
assertEquals(1, lbf.getBeanDefinitionCount());
DependenciesBean kerry = (DependenciesBean) registered;
TestBean rod = (TestBean) lbf.getBean("rod");
assertSame(rod, kerry.getSpouse());
}
public void testAutowireWithSatisfiedConstructorDependency() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
lbf.registerBeanDefinition("rod", bd);
assertEquals(1, lbf.getBeanDefinitionCount());
Object registered = lbf.autowire(
ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
assertEquals(1, lbf.getBeanDefinitionCount());
ConstructorDependency kerry = (ConstructorDependency) registered;
TestBean rod = (TestBean) lbf.getBean("rod");
assertSame(rod, kerry.spouse);
}
public void testAutowireWithUnsatisfiedConstructorDependency() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
lbf.registerBeanDefinition("rod", bd);
assertEquals(1, lbf.getBeanDefinitionCount());
try {
lbf.autowire(
UnsatisfiedConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);
fail("Should have unsatisfied constructor dependency on SideEffectBean");
}
catch (UnsatisfiedDependencyException ex) {
// Ok
}
}
public void testExtensiveCircularReference() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
for (int i = 0; i < 1000; i++) {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i+1 : 0))));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
lbf.registerBeanDefinition("bean" + i, bd);
}
lbf.preInstantiateSingletons();
for (int i = 0; i < 1000; i++) {
TestBean bean = (TestBean) lbf.getBean("bean" + i);
TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i+1 : 0));
assertTrue(bean.getSpouse() == otherBean);
}
}
public void testCircularReferenceThroughAutowiring() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("test", new RootBeanDefinition(
ConstructorDependencyBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR));
try {
lbf.preInstantiateSingletons();
}
catch (UnsatisfiedDependencyException ex) {
// expected
}
}
public void testCircularReferenceThroughFactoryBeanAutowiring() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("test", new RootBeanDefinition(
ConstructorDependencyFactoryBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR));
try {
lbf.preInstantiateSingletons();
}
catch (UnsatisfiedDependencyException ex) {
// expected
}
}
public void testApplyPropertyValues() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("age", "99");
lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class, pvs));
TestBean tb = new TestBean();
assertEquals(0, tb.getAge());
lbf.applyBeanPropertyValues(tb, "test");
assertEquals(99, tb.getAge());
}
public void testApplyPropertyValuesWithIncompleteDefinition() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("age", "99");
lbf.registerBeanDefinition("test", new RootBeanDefinition(null, pvs));
TestBean tb = new TestBean();
assertEquals(0, tb.getAge());
lbf.applyBeanPropertyValues(tb, "test");
assertEquals(99, tb.getAge());
}
public static class NoDependencies {
private NoDependencies() {
}
}
public static class ConstructorDependency {
public TestBean spouse;
public ConstructorDependency(TestBean spouse) {
this.spouse = spouse;
}
private ConstructorDependency(TestBean spouse, TestBean otherSpouse) {
throw new IllegalArgumentException("Should never be called");
}
}
public static class UnsatisfiedConstructorDependency {
public UnsatisfiedConstructorDependency(TestBean t, SideEffectBean b) {
}
}
public static class ConstructorDependencyBean {
public ConstructorDependencyBean(ConstructorDependencyBean dependency) {
}
}
public static class ConstructorDependencyFactoryBean implements FactoryBean {
public ConstructorDependencyFactoryBean(String dependency) {
}
public Object getObject() throws Exception {
return "test";
}
public Class getObjectType() {
return String.class;
}
public boolean isSingleton() {
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -