Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#registerBeanDefinition()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#registerBeanDefinition() . These examples are extracted from open source projects. 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
@Test
public void configurationWithPostProcessor() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ConfigWithPostProcessor.class);
	RootBeanDefinition placeholderConfigurer = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
	placeholderConfigurer.getPropertyValues().add("properties", "myProp=myValue");
	ctx.registerBeanDefinition("placeholderConfigurer", placeholderConfigurer);
	ctx.refresh();

	TestBean foo = ctx.getBean("foo", TestBean.class);
	ITestBean bar = ctx.getBean("bar", ITestBean.class);
	ITestBean baz = ctx.getBean("baz", ITestBean.class);

	assertEquals("foo-processed-myValue", foo.getName());
	assertEquals("bar-processed-myValue", bar.getName());
	assertEquals("baz-processed-myValue", baz.getName());

	SpousyTestBean listener = ctx.getBean("listenerTestBean", SpousyTestBean.class);
	assertTrue(listener.refreshed);
	ctx.close();
}
 
Example 2
@Test
@SuppressWarnings("resource")
public void formatFieldForValueInjectionUsingMetaAnnotations() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
	RootBeanDefinition bd = new RootBeanDefinition(MetaValueBean.class);
	bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	ac.registerBeanDefinition("valueBean", bd);
	ac.registerBeanDefinition("conversionService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
	ac.registerBeanDefinition("ppc", new RootBeanDefinition(PropertyPlaceholderConfigurer.class));
	ac.refresh();
	System.setProperty("myDate", "10-31-09");
	System.setProperty("myNumber", "99.99%");
	try {
		MetaValueBean valueBean = ac.getBean(MetaValueBean.class);
		assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date));
		assertEquals(Double.valueOf(0.9999), valueBean.number);
	}
	finally {
		System.clearProperty("myDate");
		System.clearProperty("myNumber");
	}
}
 
Example 3
public static void main(String[] args) {

        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // 生成 UserHolder 的 BeanDefinition
        BeanDefinition userHolderBeanDefinition = createUserHolderBeanDefinition();
        // 注册 UserHolder 的 BeanDefinition
        applicationContext.registerBeanDefinition("userHolder", userHolderBeanDefinition);

        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);

        String xmlResourcePath = "classpath:/META-INF/dependency-lookup-context.xml";
        // 加载 XML 资源,解析并且生成 BeanDefinition
        beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);

        // 启动 Spring 应用上下文
        applicationContext.refresh();

        // 依赖查找并且创建 Bean
        UserHolder userHolder = applicationContext.getBean(UserHolder.class);
        System.out.println(userHolder);

        // 显示地关闭 Spring 应用上下文
        applicationContext.close();
    }
 
Example 4
@Test
public void configurationWithPostProcessor() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ConfigWithPostProcessor.class);
	RootBeanDefinition placeholderConfigurer = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
	placeholderConfigurer.getPropertyValues().add("properties", "myProp=myValue");
	ctx.registerBeanDefinition("placeholderConfigurer", placeholderConfigurer);
	ctx.refresh();

	TestBean foo = ctx.getBean("foo", TestBean.class);
	ITestBean bar = ctx.getBean("bar", ITestBean.class);
	ITestBean baz = ctx.getBean("baz", ITestBean.class);

	assertEquals("foo-processed-myValue", foo.getName());
	assertEquals("bar-processed-myValue", bar.getName());
	assertEquals("baz-processed-myValue", baz.getName());

	SpousyTestBean listener = ctx.getBean("listenerTestBean", SpousyTestBean.class);
	assertTrue(listener.refreshed);
	ctx.close();
}
 
Example 5
@Test
public void configurationWithPostProcessor() {
	AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext();
	factory.register(ConfigWithPostProcessor.class);
	RootBeanDefinition placeholderConfigurer = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
	placeholderConfigurer.getPropertyValues().add("properties", "myProp=myValue");
	factory.registerBeanDefinition("placeholderConfigurer", placeholderConfigurer);
	factory.refresh();

	TestBean foo = factory.getBean("foo", TestBean.class);
	ITestBean bar = factory.getBean("bar", ITestBean.class);
	ITestBean baz = factory.getBean("baz", ITestBean.class);

	assertEquals("foo-processed-myValue", foo.getName());
	assertEquals("bar-processed-myValue", bar.getName());
	assertEquals("baz-processed-myValue", baz.getName());

	SpousyTestBean listener = factory.getBean("listenerTestBean", SpousyTestBean.class);
	assertTrue(listener.refreshed);
}
 
Example 6
@Test
@SuppressWarnings("resource")
public void formatFieldForValueInjectionUsingMetaAnnotations() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
	RootBeanDefinition bd = new RootBeanDefinition(MetaValueBean.class);
	bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	ac.registerBeanDefinition("valueBean", bd);
	ac.registerBeanDefinition("conversionService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
	ac.registerBeanDefinition("ppc", new RootBeanDefinition(PropertyPlaceholderConfigurer.class));
	ac.refresh();
	System.setProperty("myDate", "10-31-09");
	System.setProperty("myNumber", "99.99%");
	try {
		MetaValueBean valueBean = ac.getBean(MetaValueBean.class);
		assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date));
		assertEquals(Double.valueOf(0.9999), valueBean.number);
	}
	finally {
		System.clearProperty("myDate");
		System.clearProperty("myNumber");
	}
}
 
Example 7
@Test
public void testCustomWithAsm() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.registerBeanDefinition("customConfig", new RootBeanDefinition(CustomConfig.class.getName()));
	RootBeanDefinition customPojo = new RootBeanDefinition(CustomPojo.class.getName());
	customPojo.setLazyInit(true);
	ctx.registerBeanDefinition("customPojo", customPojo);
	ctx.refresh();
	assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
	assertFalse(ctx.getBeanFactory().containsSingleton("testBean2"));
	CustomPojo pojo = ctx.getBean(CustomPojo.class);
	assertThat(pojo.testBean.getName(), equalTo("interesting"));
}
 
Example 8
@Test
public void systemPropertiesSecurityManager() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();

	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(TestBean.class);
	bd.getPropertyValues().add("country", "#{systemProperties.country}");
	ac.registerBeanDefinition("tb", bd);

	SecurityManager oldSecurityManager = System.getSecurityManager();
	try {
		System.setProperty("country", "NL");

		SecurityManager securityManager = new SecurityManager() {
			@Override
			public void checkPropertiesAccess() {
				throw new AccessControlException("Not Allowed");
			}
			@Override
			public void checkPermission(Permission perm) {
				// allow everything else
			}
		};
		System.setSecurityManager(securityManager);
		ac.refresh();

		TestBean tb = ac.getBean("tb", TestBean.class);
		assertEquals("NL", tb.getCountry());

	}
	finally {
		System.setSecurityManager(oldSecurityManager);
		System.getProperties().remove("country");
	}
}
 
Example 9
@Test
public void stringConcatenationWithDebugLogging() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();

	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(String.class);
	bd.getConstructorArgumentValues().addGenericArgumentValue("test-#{ T(java.lang.System).currentTimeMillis() }");
	ac.registerBeanDefinition("str", bd);
	ac.refresh();

	String str = ac.getBean("str", String.class);
	assertTrue(str.startsWith("test-"));
}
 
Example 10
@Test
@SuppressWarnings("resource")
public void formatFieldForValueInjection() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
	ac.registerBeanDefinition("valueBean", new RootBeanDefinition(ValueBean.class));
	ac.registerBeanDefinition("conversionService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
	ac.refresh();
	ValueBean valueBean = ac.getBean(ValueBean.class);
	assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date));
}
 
Example 11
@Test
@SuppressWarnings("resource")
public void testFormatFieldForValueInjection() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
	ac.registerBeanDefinition("valueBean", new RootBeanDefinition(ValueBean.class));
	ac.registerBeanDefinition("conversionService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
	ac.refresh();
	ValueBean valueBean = ac.getBean(ValueBean.class);
	assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date));
}
 
Example 12
@Test
@SuppressWarnings("resource")
public void testNonLoadedConfigClass() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.registerBeanDefinition("config", new RootBeanDefinition(InvokerAutowiringConfig.class.getName()));
	context.refresh();
	MyBean myBean = context.getBean("myBean", MyBean.class);
	assertSame(context.getBean("myService"), myBean.myService);
	myBean.myService.handle();
	myBean.myService.handleAsync();
}
 
Example 13
public static void main(String[] args) {
    // 创建 BeanFactory 容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

    // 注册 BeanDefinition Bean Class 是一个 CharSequence 接口
    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(CharSequence.class);
    applicationContext.registerBeanDefinition("errorBean", beanDefinitionBuilder.getBeanDefinition());

    // 启动应用上下文
    applicationContext.refresh();

    // 关闭应用上下文
    applicationContext.close();
}
 
Example 14
@Test
public void testCustomWithAsm() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.registerBeanDefinition("customConfig", new RootBeanDefinition(CustomConfig.class.getName()));
	RootBeanDefinition customPojo = new RootBeanDefinition(CustomPojo.class.getName());
	customPojo.setLazyInit(true);
	ctx.registerBeanDefinition("customPojo", customPojo);
	ctx.refresh();
	assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
	CustomPojo pojo = ctx.getBean(CustomPojo.class);
	assertThat(pojo.testBean.getName(), equalTo("interesting"));
}
 
Example 15
@Test
@SuppressWarnings("resource")
public void testNonLoadedConfigClass() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.registerBeanDefinition("config", new RootBeanDefinition(InvokerAutowiringConfig.class.getName()));
	context.refresh();
	MyBean myBean = context.getBean("myBean", MyBean.class);
	assertSame(context.getBean("myService"), myBean.myService);
	myBean.myService.handle();
	myBean.myService.handleAsync();
}
 
Example 16
@Test
public void testCustomWithAsm() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.registerBeanDefinition("customConfig", new RootBeanDefinition(CustomConfig.class.getName()));
	RootBeanDefinition customPojo = new RootBeanDefinition(CustomPojo.class.getName());
	customPojo.setLazyInit(true);
	ctx.registerBeanDefinition("customPojo", customPojo);
	ctx.refresh();
	assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
	assertFalse(ctx.getBeanFactory().containsSingleton("testBean2"));
	CustomPojo pojo = ctx.getBean(CustomPojo.class);
	assertThat(pojo.testBean.getName(), equalTo("interesting"));
}
 
Example 17
@Test
public void systemPropertiesSecurityManager() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();

	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(TestBean.class);
	bd.getPropertyValues().add("country", "#{systemProperties.country}");
	ac.registerBeanDefinition("tb", bd);

	SecurityManager oldSecurityManager = System.getSecurityManager();
	try {
		System.setProperty("country", "NL");

		SecurityManager securityManager = new SecurityManager() {
			@Override
			public void checkPropertiesAccess() {
				throw new AccessControlException("Not Allowed");
			}
			@Override
			public void checkPermission(Permission perm) {
				// allow everything else
			}
		};
		System.setSecurityManager(securityManager);
		ac.refresh();

		TestBean tb = ac.getBean("tb", TestBean.class);
		assertEquals("NL", tb.getCountry());

	}
	finally {
		System.setSecurityManager(oldSecurityManager);
		System.getProperties().remove("country");
	}
}
 
Example 18
@Test
public void stringConcatenationWithDebugLogging() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();

	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(String.class);
	bd.getConstructorArgumentValues().addGenericArgumentValue("test-#{ T(java.lang.System).currentTimeMillis() }");
	ac.registerBeanDefinition("str", bd);
	ac.refresh();

	String str = ac.getBean("str", String.class);
	assertTrue(str.startsWith("test-"));
}
 
Example 19
@Test
public void withAnnotatedTransactionManagers() throws Exception {
	AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
	parent.registerBeanDefinition("transactionManager1", new RootBeanDefinition(SynchTransactionManager.class));
	parent.registerBeanDefinition("transactionManager2", new RootBeanDefinition(NoSynchTransactionManager.class));
	parent.refresh();
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent);
	doTestWithMultipleTransactionManagers(context);
}
 
Example 20
@Test
public void test() {

    final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

    applicationContext.register(getClass());

    Class<?> processorClass = DubboConfigBindingBeanPostProcessor.class;

    applicationContext.registerBeanDefinition("DubboConfigBindingBeanPostProcessor", rootBeanDefinition(processorClass).addConstructorArgValue("dubbo.application").addConstructorArgValue("applicationBean").getBeanDefinition());

    applicationContext.refresh();

    ApplicationConfig applicationConfig = applicationContext.getBean(ApplicationConfig.class);

    Assert.assertEquals("dubbo-demo-application", applicationConfig.getName());

}