Java Code Examples for org.springframework.beans.factory.support.RootBeanDefinition#setFactoryBeanName()

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setFactoryBeanName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringUtil.java    From learnjavabug with MIT License 6 votes vote down vote up
public static BeanFactory makeMethodTrigger(Object o, String method) throws Exception {
  DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
  RootBeanDefinition caller = new RootBeanDefinition();

  caller.setFactoryBeanName("obj");
  caller.setFactoryMethodName(method);
  Reflections.setFieldValue(caller.getMethodOverrides(), "overrides", new HashSet<>());
  bf.registerBeanDefinition("caller", caller);

  Reflections.getField(DefaultListableBeanFactory.class, "beanClassLoader").set(bf, null);
  Reflections.getField(DefaultListableBeanFactory.class, "alreadyCreated")
      .set(bf, new HashSet<>());
  Reflections.getField(DefaultListableBeanFactory.class, "singletonsCurrentlyInCreation")
      .set(bf, new HashSet<>());
  Reflections.getField(DefaultListableBeanFactory.class, "inCreationCheckExclusions")
      .set(bf, new HashSet<>());
  Reflections.getField(DefaultListableBeanFactory.class, "logger").set(bf, new NoOpLog());
  Reflections.getField(DefaultListableBeanFactory.class, "prototypesCurrentlyInCreation")
      .set(bf, new ThreadLocal<>());

  @SuppressWarnings("unchecked")
  Map<String, Object> objs = (Map<String, Object>) Reflections
      .getFieldValue(bf, "singletonObjects");
  objs.put("obj", o);
  return bf;
}
 
Example 2
Source File: SpringUtil.java    From marshalsec with MIT License 6 votes vote down vote up
public static BeanFactory makeMethodTrigger ( Object o, String method ) throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    RootBeanDefinition caller = new RootBeanDefinition();

    caller.setFactoryBeanName("obj");
    caller.setFactoryMethodName(method);
    Reflections.setFieldValue(caller.getMethodOverrides(), "overrides", new HashSet<>());
    bf.registerBeanDefinition("caller", caller);

    Reflections.getField(DefaultListableBeanFactory.class, "beanClassLoader").set(bf, null);
    Reflections.getField(DefaultListableBeanFactory.class, "alreadyCreated").set(bf, new HashSet<>());
    Reflections.getField(DefaultListableBeanFactory.class, "singletonsCurrentlyInCreation").set(bf, new HashSet<>());
    Reflections.getField(DefaultListableBeanFactory.class, "inCreationCheckExclusions").set(bf, new HashSet<>());
    Reflections.getField(DefaultListableBeanFactory.class, "logger").set(bf, new NoOpLog());
    Reflections.getField(DefaultListableBeanFactory.class, "prototypesCurrentlyInCreation").set(bf, new ThreadLocal<>());

    @SuppressWarnings ( "unchecked" )
    Map<String, Object> objs = (Map<String, Object>) Reflections.getFieldValue(bf, "singletonObjects");
    objs.put("obj", o);
    return bf;
}
 
Example 3
Source File: BindingBeanDefinitionRegistryUtils.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
private static void registerBindingTargetBeanDefinition(
		Class<? extends Annotation> qualifier, String qualifierValue, String name,
		String bindingTargetInterfaceBeanName,
		String bindingTargetInterfaceMethodName, BeanDefinitionRegistry registry) {

	if (registry.containsBeanDefinition(name)) {
		throw new BeanDefinitionStoreException(bindingTargetInterfaceBeanName, name,
				"bean definition with this name already exists - "
						+ registry.getBeanDefinition(name));
	}

	RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
	rootBeanDefinition.setFactoryBeanName(bindingTargetInterfaceBeanName);
	rootBeanDefinition.setUniqueFactoryMethodName(bindingTargetInterfaceMethodName);
	rootBeanDefinition
			.addQualifier(new AutowireCandidateQualifier(qualifier, qualifierValue));
	registry.registerBeanDefinition(name, rootBeanDefinition);
}
 
Example 4
Source File: RequiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testWithFactoryBean() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	RootBeanDefinition beanDef = new RootBeanDefinition(RequiredTestBean.class);
	beanDef.setFactoryBeanName("testBeanFactory");
	beanDef.setFactoryMethodName("create");
	factory.registerBeanDefinition("testBean", beanDef);
	factory.registerBeanDefinition("testBeanFactory", new RootBeanDefinition(RequiredTestBeanFactory.class));
	RequiredAnnotationBeanPostProcessor bpp = new RequiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(factory);
	factory.addBeanPostProcessor(bpp);
	factory.preInstantiateSingletons();
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertFalse(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 6
Source File: RequiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testWithFactoryBean() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	RootBeanDefinition beanDef = new RootBeanDefinition(RequiredTestBean.class);
	beanDef.setFactoryBeanName("testBeanFactory");
	beanDef.setFactoryMethodName("create");
	factory.registerBeanDefinition("testBean", beanDef);
	factory.registerBeanDefinition("testBeanFactory", new RootBeanDefinition(RequiredTestBeanFactory.class));
	RequiredAnnotationBeanPostProcessor bpp = new RequiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(factory);
	factory.addBeanPostProcessor(bpp);
	factory.preInstantiateSingletons();
}
 
Example 7
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertFalse(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 8
Source File: RequiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithFactoryBean() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	RootBeanDefinition beanDef = new RootBeanDefinition(RequiredTestBean.class);
	beanDef.setFactoryBeanName("testBeanFactory");
	beanDef.setFactoryMethodName("create");
	factory.registerBeanDefinition("testBean", beanDef);
	factory.registerBeanDefinition("testBeanFactory", new RootBeanDefinition(RequiredTestBeanFactory.class));
	RequiredAnnotationBeanPostProcessor bpp = new RequiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(factory);
	factory.addBeanPostProcessor(bpp);
	factory.preInstantiateSingletons();
}
 
Example 9
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertFalse(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 10
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition();
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertFalse(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertFalse(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertFalse(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertFalse(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertFalse(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertFalse(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(null, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}
 
Example 11
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition();
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertFalse(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertFalse(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertFalse(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertFalse(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertFalse(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertFalse(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(null, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}
 
Example 12
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition();
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertFalse(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertFalse(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertFalse(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertFalse(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertFalse(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertFalse(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(null, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}