Search in sources :

Example 41 with BeansException

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);
    }
}
Also used : StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) BeanExpressionException(org.springframework.beans.factory.BeanExpressionException) StandardTypeLocator(org.springframework.expression.spel.support.StandardTypeLocator) Expression(org.springframework.expression.Expression) ConversionService(org.springframework.core.convert.ConversionService) BeanExpressionException(org.springframework.beans.factory.BeanExpressionException) BeansException(org.springframework.beans.BeansException)

Example 42 with BeansException

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();
}
Also used : HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) DummyFactory(org.springframework.tests.sample.beans.factory.DummyFactory) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) HashMap(java.util.HashMap) Map(java.util.Map) BeansException(org.springframework.beans.BeansException) Before(org.junit.Before)

Example 43 with BeansException

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);
                    }
                }
            }
        }
    });
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) BeansException(org.springframework.beans.BeansException)

Example 44 with BeansException

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());
    }
}
Also used : InstantiationAwareBeanPostProcessorAdapter(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyValue(org.springframework.beans.PropertyValue) ParseException(java.text.ParseException) TypeMismatchException(org.springframework.beans.TypeMismatchException) NotWritablePropertyException(org.springframework.beans.NotWritablePropertyException) ExpectedException(org.junit.rules.ExpectedException) MalformedURLException(java.net.MalformedURLException) BeansException(org.springframework.beans.BeansException) Field(java.lang.reflect.Field) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeansException(org.springframework.beans.BeansException)

Example 45 with BeansException

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
    }
}
Also used : BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Aggregations

BeansException (org.springframework.beans.BeansException)73 Test (org.junit.Test)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)14 BeanWrapper (org.springframework.beans.BeanWrapper)11 ApplicationContext (org.springframework.context.ApplicationContext)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)9 BeanCreationException (org.springframework.beans.factory.BeanCreationException)8 MalformedURLException (java.net.MalformedURLException)7 ArrayList (java.util.ArrayList)7 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)7 Map (java.util.Map)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)5 IOException (java.io.IOException)4 Method (java.lang.reflect.Method)4 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)4 UrlResource (org.springframework.core.io.UrlResource)4 File (java.io.File)3