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

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#registerBeanDefinition() . 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: ConfigurationClassProcessingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: FormattingConversionServiceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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
Source File: ConfigurationClassProcessingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: FormattingConversionServiceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 5
Source File: ConfigurationClassProcessingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 6
Source File: ApiDependencySetterInjectionDemo.java    From geekbang-lessons with Apache License 2.0 6 votes vote down vote up
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 7
Source File: AnnotationDrivenTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 8
Source File: ApplicationContextExpressionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 9
Source File: ApplicationContextExpressionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 10
Source File: BeanMethodQualificationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 11
Source File: HttpInvokerFactoryBeanIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 12
Source File: BeanMethodQualificationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: BeanInstantiationExceptionDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
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
Source File: HttpInvokerFactoryBeanIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 15
Source File: FormattingConversionServiceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 16
Source File: FormattingConversionServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 17
Source File: ApplicationContextExpressionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 18
Source File: ApplicationContextExpressionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 19
Source File: BeanMethodQualificationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 20
Source File: DubboConfigBindingBeanPostProcessorTest.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@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());

}