use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class StandardBeanExpressionResolver method evaluate.
@Override
public Object evaluate(String value, BeanExpressionContext evalContext) throws BeansException {
if (!StringUtils.hasLength(value)) {
return value;
}
try {
Expression expr = this.expressionCache.get(value);
if (expr == null) {
expr = this.expressionParser.parseExpression(value, this.beanExpressionParserContext);
this.expressionCache.put(value, expr);
}
StandardEvaluationContext sec = this.evaluationCache.get(evalContext);
if (sec == null) {
sec = new StandardEvaluationContext();
sec.setRootObject(evalContext);
sec.addPropertyAccessor(new BeanExpressionContextAccessor());
sec.addPropertyAccessor(new BeanFactoryAccessor());
sec.addPropertyAccessor(new MapAccessor());
sec.addPropertyAccessor(new EnvironmentAccessor());
sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
ConversionService conversionService = evalContext.getBeanFactory().getConversionService();
if (conversionService != null) {
sec.setTypeConverter(new StandardTypeConverter(conversionService));
}
customizeEvaluationContext(sec);
this.evaluationCache.put(evalContext, sec);
}
return expr.getValue(sec);
} catch (Exception ex) {
throw new BeanExpressionException("Expression parsing failed", ex);
}
}
use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class XmlListableBeanFactoryTests method setUp.
@Before
public void setUp() {
parent = new DefaultListableBeanFactory();
Map m = new HashMap();
m.put("name", "Albert");
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setPropertyValues(new MutablePropertyValues(m));
parent.registerBeanDefinition("father", bd1);
m = new HashMap();
m.put("name", "Roderick");
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
bd2.setPropertyValues(new MutablePropertyValues(m));
parent.registerBeanDefinition("rod", bd2);
this.factory = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
this.factory.addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
if (bean instanceof TestBean) {
((TestBean) bean).setPostProcessed(true);
}
if (bean instanceof DummyFactory) {
((DummyFactory) bean).setPostProcessed(true);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
return bean;
}
});
this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
//this.factory.preInstantiateSingletons();
}
use of org.springframework.beans.BeansException in project spring-boot by spring-projects.
the class WebDriverScope method registerWith.
/**
* Register this scope with the specified context and reassign appropriate bean
* definitions to used it.
* @param context the application context
*/
public static void registerWith(ConfigurableApplicationContext context) {
if (!ClassUtils.isPresent(WEB_DRIVER_CLASS, null)) {
return;
}
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
if (beanFactory.getRegisteredScope(NAME) == null) {
beanFactory.registerScope(NAME, new WebDriverScope());
}
context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
for (String beanClass : BEAN_CLASSES) {
for (String beanName : beanFactory.getBeanNamesForType(ClassUtils.resolveClassName(beanClass, null))) {
BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
if (!StringUtils.hasLength(definition.getScope())) {
definition.setScope(NAME);
}
}
}
}
});
}
use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method doTestFieldSettingWithInstantiationAwarePostProcessor.
private void doTestFieldSettingWithInstantiationAwarePostProcessor(final boolean skipPropertyPopulation) {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
int ageSetByPropertyValue = 27;
bd.getPropertyValues().addPropertyValue(new PropertyValue("age", new Integer(ageSetByPropertyValue)));
lbf.registerBeanDefinition("test", bd);
final String nameSetOnField = "nameSetOnField";
lbf.addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
TestBean tb = (TestBean) bean;
try {
Field f = TestBean.class.getDeclaredField("name");
f.setAccessible(true);
f.set(tb, nameSetOnField);
return !skipPropertyPopulation;
} catch (Exception ex) {
fail("Unexpected exception: " + ex);
// Keep compiler happy about return
throw new IllegalStateException();
}
}
});
lbf.preInstantiateSingletons();
TestBean tb = (TestBean) lbf.getBean("test");
assertEquals("Name was set on field by IAPP", nameSetOnField, tb.getName());
if (!skipPropertyPopulation) {
assertEquals("Property value still set", ageSetByPropertyValue, tb.getAge());
} else {
assertEquals("Property value was NOT set and still has default value", 0, tb.getAge());
}
}
use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class CustomEditorTests method testDefaultBooleanEditorForPrimitiveType.
@Test
public void testDefaultBooleanEditorForPrimitiveType() {
BooleanTestBean tb = new BooleanTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.setPropertyValue("bool1", "true");
assertTrue("Correct bool1 value", Boolean.TRUE.equals(bw.getPropertyValue("bool1")));
assertTrue("Correct bool1 value", tb.isBool1());
bw.setPropertyValue("bool1", "false");
assertTrue("Correct bool1 value", Boolean.FALSE.equals(bw.getPropertyValue("bool1")));
assertTrue("Correct bool1 value", !tb.isBool1());
bw.setPropertyValue("bool1", " true ");
assertTrue("Correct bool1 value", tb.isBool1());
bw.setPropertyValue("bool1", " false ");
assertTrue("Correct bool1 value", !tb.isBool1());
bw.setPropertyValue("bool1", "on");
assertTrue("Correct bool1 value", tb.isBool1());
bw.setPropertyValue("bool1", "off");
assertTrue("Correct bool1 value", !tb.isBool1());
bw.setPropertyValue("bool1", "yes");
assertTrue("Correct bool1 value", tb.isBool1());
bw.setPropertyValue("bool1", "no");
assertTrue("Correct bool1 value", !tb.isBool1());
bw.setPropertyValue("bool1", "1");
assertTrue("Correct bool1 value", tb.isBool1());
bw.setPropertyValue("bool1", "0");
assertTrue("Correct bool1 value", !tb.isBool1());
try {
bw.setPropertyValue("bool1", "argh");
fail("Should have thrown BeansException");
} catch (BeansException ex) {
// expected
}
}
Aggregations