Spring表达式语言不适用于spring aop(Spring expression language not working with spring aop)

编程入门 行业动态 更新时间:2024-10-27 03:34:08
Spring表达式语言不适用于spring aop(Spring expression language not working with spring aop) <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>

在上面的代码中,我使用spring表达式语言注入了eddie bean的song , instrument属性。 但是, song属性没有正确注入..我得到以下错误:

线程“main”org.springframework.beans.factory.BeanCreationException中的异常:在类路径资源[spring-config.xml]中定义名称为'eddie'的bean时出错:bean的初始化失败; 嵌套异常是org.springframework.beans.factory.BeanExpressionException:表达式解析失败; 嵌套异常是org.springframework.expression.spel.SpelEvaluationException:EL1008E:(pos 6):在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory上的'$ Proxy4'类型的对象上找不到字段或属性'song'。 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)中的doCreateBean(AbstractAutowireCapableBeanFactory.java:519)位于org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:290) )org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)位于org.springframework.beans的org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)。 factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFa) ctory.java:557)atg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)位于org.springframework.context的org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416) .support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)位于com.springinaction.Main.main(Main.java:10)的org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)。

如果没有注入歌曲属性,则会正确注入乐器属性,因为只有aop才会发生这种情况。

当我评论<aop:config>它工作正常..

哪里不对了?

<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?

最满意答案

你试过了吗

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

这样您就可以获得动态子类,并且该属性应该在通过Spring AOP创建的代理中可用。

Spring AOP的默认行为是为接口创建Java代理,因此无法通过代理访问任何类的属性。

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.

更多推荐

本文发布于:2023-07-30 01:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   不适用于   语言   Spring   spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!