use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.
the class RequiredAnnotationBeanPostProcessorTests method testWithCustomAnnotation.
@Test
public void testWithCustomAnnotation() {
try {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).getBeanDefinition();
factory.registerBeanDefinition("testBean", beanDef);
RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor();
rabpp.setRequiredAnnotationType(MyRequired.class);
factory.addBeanPostProcessor(rabpp);
factory.preInstantiateSingletons();
fail("Should have thrown BeanCreationException");
} catch (BeanCreationException ex) {
String message = ex.getCause().getMessage();
assertTrue(message.contains("Property"));
assertTrue(message.contains("name"));
assertTrue(message.contains("testBean"));
}
}
use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.
the class RequiredAnnotationBeanPostProcessorTests method testWithAllRequiredPropertiesSpecified.
@Test
public void testWithAllRequiredPropertiesSpecified() {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).addPropertyValue("age", "24").addPropertyValue("favouriteColour", "Blue").addPropertyValue("jobTitle", "Grand Poobah").getBeanDefinition();
factory.registerBeanDefinition("testBean", beanDef);
factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
factory.preInstantiateSingletons();
RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
assertEquals(24, bean.getAge());
assertEquals("Blue", bean.getFavouriteColour());
}
use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.
the class RequiredAnnotationBeanPostProcessorTests method testWithStaticFactoryMethodAndRequiredPropertiesSpecified.
@Test
public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).setFactoryMethod("create").addPropertyValue("age", "24").addPropertyValue("favouriteColour", "Blue").addPropertyValue("jobTitle", "Grand Poobah").getBeanDefinition();
factory.registerBeanDefinition("testBean", beanDef);
factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
factory.preInstantiateSingletons();
RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
assertEquals(24, bean.getAge());
assertEquals("Blue", bean.getFavouriteColour());
}
use of org.springframework.beans.factory.config.BeanDefinition in project camel by apache.
the class CamelNamespaceHandler method registerEndpoint.
private void registerEndpoint(Element childElement, ParserContext parserContext, String contextId) {
String id = childElement.getAttribute("id");
// must have an id to be registered
if (ObjectHelper.isNotEmpty(id)) {
BeanDefinition definition = endpointParser.parse(childElement, parserContext);
definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
// Need to add this dependency of CamelContext for Spring 3.0
try {
Method method = definition.getClass().getMethod("setDependsOn", String[].class);
method.invoke(definition, (Object) new String[] { contextId });
} catch (Exception e) {
// Do nothing here
}
parserContext.registerBeanComponent(new BeanComponentDefinition(definition, id));
}
}
use of org.springframework.beans.factory.config.BeanDefinition in project camel by apache.
the class CamelNamespaceHandler method addDependsOnToRouteBuilder.
private void addDependsOnToRouteBuilder(Element childElement, ParserContext parserContext, String contextId) {
// setting the depends-on explicitly is required since Spring 3.0
String routeBuilderName = childElement.getAttribute("ref");
if (ObjectHelper.isNotEmpty(routeBuilderName)) {
// set depends-on to the context for a routeBuilder bean
try {
BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(routeBuilderName);
Method getDependsOn = definition.getClass().getMethod("getDependsOn", new Class[] {});
String[] dependsOn = (String[]) getDependsOn.invoke(definition);
if (dependsOn == null || dependsOn.length == 0) {
dependsOn = new String[] { contextId };
} else {
String[] temp = new String[dependsOn.length + 1];
System.arraycopy(dependsOn, 0, temp, 0, dependsOn.length);
temp[dependsOn.length] = contextId;
dependsOn = temp;
}
Method method = definition.getClass().getMethod("setDependsOn", String[].class);
method.invoke(definition, (Object) dependsOn);
} catch (Exception e) {
// Do nothing here
}
}
}
Aggregations