views:

36

answers:

1
<bean id="eddie" class="com.springinaction.Instrumentalist">
    <property name="instrument" value="#{violin}"></property>
    <property name="song" value="#{kenny.song}"></property>

</bean>

<bean id="violin" class="com.springinaction.Violin">
</bean>


<bean id="kenny" class="com.springinaction.Instrumentalist">
    <property name="song" value="Kenny is a star,kenny is a star"></property>
    <property name="instrument" ref="saxopone"></property>
</bean>

<aop:config>

    <aop:aspect ref="audience">

        <aop:before pointcut="execution(* com.springinaction.Performer.perform(..))" method="takeSeats()"/>

        <aop:after-throwing method="demandRefund" pointcut="execution(* com.springinaction.Performer.perform(..))"/>

    </aop:aspect>

</aop:config>

In the above code,I am injecting song , instrument property of eddie bean using spring expression language. But, song property not injected properly..and i am getting the below error:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eddie' defined in class path resource [spring-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 6): Field or property 'song' cannot be found on object of type '$Proxy4' at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at com.springinaction.Main.main(Main.java:10)

Instrument property is injected properly where as song property is not injected and this is happening because of aop only..

when i comment out <aop:config> it is working fine..

Anything wrong?

+2  A: 

Did you try

<aop:config proxy-target-class="true">
...
</aop:config>

This way you get a dynamic subclass and the property should be available in the proxy created via Spring AOP.

The default behaviour of Spring AOP is to create a Java proxy for the interfaces, so the properties of any classes won't be accessible through the proxy.

Timo Westkämper
Yes, it is working..thanks for your time Timo..Spring AOP really eating my brain :)
javanoob
`Spring in Action` book does not talk about all these details.I think i have to read spring reference..
javanoob
yes, better read the reference on these things.
Timo Westkämper