Spring配置文件里如何同时配置Spel表达和AOP

[align=center][color=red] [size=x-large]Spring配置文件里如何同时配置Spel表达和AOP[/size][/color]
[/align]
原文地址:http://blog.csdn.net/gxlstone/article/details/12955063

[color=darkred][size=large][size=small][size=small]你的程序应该报如下错误了吧,否则你了不会来这看我看这破玩意!
注意红色文字,我们来猜它的大概意思,毕竟英语不是很好嘛(好吧,根本就不好!)。
在类型为'$Proxy4'的对象上找不到song属性。
越过如下这段错误堆栈,让我们继续看后面的汉字。[/size][/size][/size][/color]
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 6): Field or property 'song' cannot be found on object of type '$Proxy4'
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:207)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:71)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:57)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:88)
at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:138)
... 19 more
在没有下面黄色代码时,我的程序跑的好好的,自从加了黄色代码,程序就像屎一样了:报错。
把如下红色代码删除的话,程序也好了。
难道红黄真的不能共存吗?当然不是,至少肛裂时,红黄就共存了,大笑
报错的原因到底是什么呢?
原来Sprig AOP默认情况下创建的代理是基于接口的,所以类的属性是不能通过代理来访问的,这就是为什么前面报这样的错了:Field or property 'song' cannot be found on object of type '$Proxy4'
那怎样创建出来的代理才能访问类的属性呢?当然是基于类的啦!
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist"
p:song="Jingle Bells" p:instrument-ref="piano">
<constructor-arg value="kenny" />
</bean>
<bean id="carl" class="com.springinaction.springidol.Instrumentalist">
<constructor-arg value="carl" />
<property name="song" value="#{kenny.song}" />
<property name="instrument" value="#{kenny.getInstrument()}" />
</bean>
<bean id="audience" class="com.springinaction.springidol.Audience" />

<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance"
expression="execution(* com.springinaction.springidol.Performer.perform(..))" />
<aop:before pointcut-ref="performance" method="takeSeats" />
<aop:before pointcut-ref="performance" method="turnOffCellPhones" />
<aop:after-returning pointcut-ref="performance"
method="applaud" />
<aop:after-throwing pointcut-ref="performance"
method="demandRefund" />
</aop:aspect>
</aop:config>

看下面的代码,尤其是红字部分。这样声明一样,就会创建基于类的动态代码了,就可以访问类的属性了。
本文中的错误也就解决了。
<aop:config [color=red]proxy-target-class="true"[/color]>
...
</aop:config>

如下错误信息好多吧,我们不看,只看上面就行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值