下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#registerBeanDefinition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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();
}
@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");
}
}
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();
}
@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();
}
@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);
}
@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");
}
}
@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"));
}
@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");
}
}
@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-"));
}
@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));
}
@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));
}
@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();
}
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();
}
@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"));
}
@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();
}
@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"));
}
@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");
}
}
@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-"));
}
@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);
}
@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());
}