Announcement Announcement Module
Collapse
No announcement yet.
Could not autowire field: private java.lang.String, @Value annoation, inner bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Could not autowire field: private java.lang.String, @Value annoation, inner bean

    Hi

    I'm having problem with @Value annotation and SpringEL when trying to set value of an inner bean property.

    As you can see below my application context contains declaration of two beans:
    - topLevelBean
    - holder bean

    As you can see SampleBean has annotated property called "property" which is set fine when used from top level bean but when I try to use this class as inner bean I'm getting an exception.

    I think that this problem could be solved by applying patch below.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:p="http://www.springframework.org/schema/p"
    	
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
    	>
    
    	<context:annotation-config/>
    	
    	<util:properties id="settings">
    		<prop key="someProperty">bla bla</prop>
    	</util:properties>
    	
    	<bean class="test.SampleBean" id="topLevelBean" />
    	
    	<bean class="test.SampleBeanHolder" id="holder">
    		<property name="sampleBean">
    			<!--  this inner bean causes an exception -->
    			<bean class="test.SampleBean" />
    		</property>
    	</bean>
    	
    </beans>
    Code:
    package test;
    
    public class SampleBeanHolder {
    
        private SampleBean sampleBean;
        
        public void setSampleBean(SampleBean sampleBean) {
            this.sampleBean = sampleBean;
        }
        
        public SampleBean getSampleBean() {
            return sampleBean;
        }
        
    }
    Code:
    package test;
    
    import org.springframework.beans.factory.annotation.Value;
    
    public class SampleBean {
    
        @Value("#{settings.someProperty}")
        private String property;
        
        public String getProperty() {
            return property;
        }
    }
    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'holder' defined in file [E:\bedkowsk\projects\migrations\migration_tools\TariffMig\tmp\bla.xml]: Cannot create inner bean 'test.SampleBean#1f26605' of type [test.SampleBean] while setting bean property 'sampleBean'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'test.SampleBean#1f26605': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String test.SampleBean.property; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'test.SampleBean#1f26605' is defined
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:281)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:120)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1305)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:558)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:852)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:422)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    	at Bla.main(Bla.java:7)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'test.SampleBean#1f26605': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String test.SampleBean.property; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'test.SampleBean#1f26605' is defined
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270)
    	... 15 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String test.SampleBean.property; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'test.SampleBean#1f26605' is defined
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:500)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:280)
    	... 19 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'test.SampleBean#1f26605' is defined
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:505)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1040)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:826)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:692)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:681)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.resolvedCachedArgument(AutowiredAnnotationBeanPostProcessor.java:429)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.access$0(AutowiredAnnotationBeanPostProcessor.java:425)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:462)
    	... 21 more
    Code:
    diff spring-framework-3.0.0.RC2/src/org/springframework/beans/factory/support/AbstractBeanFactory.java workspace/spring/src/org/springframework/beans/factory/support/AbstractBeanFactory.java
    1257c1257,1262
    < 		Scope scope = getRegisteredScope(beanDefinition.getScope());
    ---
    > 		Scope scope;
    > 		if( beanDefinition != null ) {
    > 			scope = getRegisteredScope(beanDefinition.getScope());
    > 		} else {
    > 			scope = getRegisteredScope(SCOPE_PROTOTYPE);
    > 		}
    diff spring-framework-3.0.0.RC2/src/org/springframework/beans/factory/support/DefaultListableBeanFactory.java workspace/spring/src/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
    692c692,696
    < 				value = evaluateBeanDefinitionString(strVal, getMergedBeanDefinition(beanName));
    ---
    > 				try {
    > 					value = evaluateBeanDefinitionString(strVal, getMergedBeanDefinition(beanName));
    > 				} catch( NoSuchBeanDefinitionException e ) { // this means it's an inner bean
    > 					value = evaluateBeanDefinitionString(strVal, null );
    > 				}
Working...
X