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 | 1 year ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  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 | 1 year 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 | 1 year 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

    Error processing condition on de.codecentric.boot.admin.config.AdminServerWebConfiguration.adminServerProperties

    GitHub | 8 months ago | magnusheino
    java.lang.IllegalStateException: Error processing condition on de.codecentric.boot.admin.config.AdminServerWebConfiguration.adminServerProperties
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    How to delete Serializable entity exposed with Spring data rest and spring data mongo

    Stack Overflow | 1 year ago | Gerald
    java.lang.IllegalArgumentException: Target bean is not of type of the persistent entity!

  1. SteVio1989 2 times, last 1 week ago
  2. Andreas Häber 1 times, last 1 week ago
  3. tomthomas 4 times, last 3 days ago
  4. Dunmer 3 times, last 4 weeks ago
  5. Axelo 1 times, last 1 month ago
10 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