org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emptyOptional' defined in URL [file:configuration/spring-configuration/components/observers.xml]: Bean instantiation via factory method failed; nested exception is java.lang.IllegalArgumentException: Optional value must be present

Spring JIRA | Yibo Wang | 8 months ago
  1. 0

    A bean who refers to an empty java.util.Optional value cannot be created with Spring configuration xml. In Spring config xml file I have: {code:xml} <bean id="emptyOptional" class="java.util.Optional" factory-method="empty"/> {code} Then when the Spring config was loaded, it reported the following error: {noformat} [junit] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emptyOptional' defined in URL [file:configuration/spring-configuration/components/observers.xml]: Bean instantiation via factory method failed; nested exception is java.lang.IllegalArgumentException: Optional value must be present [junit] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [junit] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [junit] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [junit] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [junit] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [junit] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) [junit] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) [junit] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) [junit] at com.amazon.gpi.componentTesting.ComponentTestContext.createContext(ComponentTestContext.java:140) [junit] at com.amazon.gpi.componentTesting.ComponentTestContext.<init>(ComponentTestContext.java:117) [junit] at com.amazon.gpi.componentTesting.ComponentTestContext.fromProfile(ComponentTestContext.java:59) [junit] at com.amazon.gpi.componentTesting.TestProfileRunner.<init>(TestProfileRunner.java:130) [junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [junit] Caused by: java.lang.IllegalArgumentException: Optional value must be present [junit] at org.springframework.util.Assert.isTrue(Assert.java:68) [junit] at org.springframework.beans.AbstractNestablePropertyAccessor$OptionalUnwrapper.unwrap(AbstractNestablePropertyAccessor.java:1049) [junit] at org.springframework.beans.AbstractNestablePropertyAccessor.setWrappedInstance(AbstractNestablePropertyAccessor.java:207) [junit] at org.springframework.beans.BeanWrapperImpl.setWrappedInstance(BeanWrapperImpl.java:138) [junit] at org.springframework.beans.AbstractNestablePropertyAccessor.setWrappedInstance(AbstractNestablePropertyAccessor.java:194) [junit] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:595) {noformat} Looks like spring tried to extract the nested value of the Optional class and use that to assign to the bean which was not what I intended. (What I tried to do was to have both empty Optional and present Optional and later conditionally choose to use one of them. So if spring did not extract the nested value, it would just work (like if the Optional is from Guava.)) Thanks

    Spring JIRA | 8 months ago | Yibo Wang
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emptyOptional' defined in URL [file:configuration/spring-configuration/components/observers.xml]: Bean instantiation via factory method failed; nested exception is java.lang.IllegalArgumentException: Optional value must be present
  2. 0

    A bean who refers to an empty java.util.Optional value cannot be created with Spring configuration xml. In Spring config xml file I have: {code:xml} <bean id="emptyOptional" class="java.util.Optional" factory-method="empty"/> {code} Then when the Spring config was loaded, it reported the following error: {noformat} [junit] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emptyOptional' defined in URL [file:configuration/spring-configuration/components/observers.xml]: Bean instantiation via factory method failed; nested exception is java.lang.IllegalArgumentException: Optional value must be present [junit] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [junit] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [junit] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [junit] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [junit] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [junit] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) [junit] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) [junit] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) [junit] at com.amazon.gpi.componentTesting.ComponentTestContext.createContext(ComponentTestContext.java:140) [junit] at com.amazon.gpi.componentTesting.ComponentTestContext.<init>(ComponentTestContext.java:117) [junit] at com.amazon.gpi.componentTesting.ComponentTestContext.fromProfile(ComponentTestContext.java:59) [junit] at com.amazon.gpi.componentTesting.TestProfileRunner.<init>(TestProfileRunner.java:130) [junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [junit] Caused by: java.lang.IllegalArgumentException: Optional value must be present [junit] at org.springframework.util.Assert.isTrue(Assert.java:68) [junit] at org.springframework.beans.AbstractNestablePropertyAccessor$OptionalUnwrapper.unwrap(AbstractNestablePropertyAccessor.java:1049) [junit] at org.springframework.beans.AbstractNestablePropertyAccessor.setWrappedInstance(AbstractNestablePropertyAccessor.java:207) [junit] at org.springframework.beans.BeanWrapperImpl.setWrappedInstance(BeanWrapperImpl.java:138) [junit] at org.springframework.beans.AbstractNestablePropertyAccessor.setWrappedInstance(AbstractNestablePropertyAccessor.java:194) [junit] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:595) {noformat} Looks like spring tried to extract the nested value of the Optional class and use that to assign to the bean which was not what I intended. (What I tried to do was to have both empty Optional and present Optional and later conditionally choose to use one of them. So if spring did not extract the nested value, it would just work (like if the Optional is from Guava.)) Thanks

    Spring JIRA | 8 months ago | Yibo Wang
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emptyOptional' defined in URL [file:configuration/spring-configuration/components/observers.xml]: Bean instantiation via factory method failed; nested exception is java.lang.IllegalArgumentException: Optional value must be present
  3. 0

    Spring Boot - Windows - Default Job 307: Job result summary - Spring CI

    spring.io | 1 year ago
    java.lang.IllegalArgumentException: Only the target location may be specified
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    How to return JSON data from spring Controller using @ResponseBody

    Stack Overflow | 1 year ago | Zahid Khan
    java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList
  6. 0

    CLI causes error with description option having white spaces

    GitHub | 1 year ago | nobeans
    java.lang.IllegalArgumentException: Only the target location may be specified

  1. kjhdofjosvs 1 times, last 3 weeks ago
  2. j.kienzle 1 times, last 1 month ago
  3. Luka 2 times, last 2 months ago
  4. andyglick 1 times, last 3 months ago
  5. qavid 2 times, last 4 months ago
3 more registered users
29 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.lang.IllegalArgumentException

    Optional value must be present

    at org.springframework.util.Assert.isTrue()
  2. Spring Core
    Assert.isTrue
    1. org.springframework.util.Assert.isTrue(Assert.java:68)
    1 frame
  3. Spring Beans
    ConstructorResolver.instantiateUsingFactoryMethod
    1. org.springframework.beans.AbstractNestablePropertyAccessor$OptionalUnwrapper.unwrap(AbstractNestablePropertyAccessor.java:1049)
    2. org.springframework.beans.AbstractNestablePropertyAccessor.setWrappedInstance(AbstractNestablePropertyAccessor.java:207)
    3. org.springframework.beans.BeanWrapperImpl.setWrappedInstance(BeanWrapperImpl.java:138)
    4. org.springframework.beans.AbstractNestablePropertyAccessor.setWrappedInstance(AbstractNestablePropertyAccessor.java:194)
    5. org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:595)
    5 frames