`

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

 
阅读更多
Spring配置文件里如何同时配置Spel表达和AOP

原文地址:http://blog.csdn.net/gxlstone/article/details/12955063

[size=small][size=small]你的程序应该报如下错误了吧,否则你了不会来这看我看这破玩意!
注意红色文字,我们来猜它的大概意思,毕竟英语不是很好嘛(好吧,根本就不好!)。
在类型为'$Proxy4'的对象上找不到song属性。
越过如下这段错误堆栈,让我们继续看后面的汉字。
[/size][/size]

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 proxy-target-class="true">
...
</aop:config>

如下错误信息好多吧,我们不看,只看上面就行
分享到:
评论

相关推荐

    spring4示例代码

    spring-2 演示了外部配置文件的引入(connection),spel(spring el)表达式 ,静态工厂方式及实例工厂方式及factorybean方式创建bean, spring的生命周期及BeanPostProcessor的使用,注解方式创建bean 及使用...

    spring4.1核心包

    配置文件 创建和管理bean。 4.spring-context-4.1.1.RELEASE.jar 在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的...

    尚硅谷佟刚Spring4代码及PPT.rar

    代码及ppt涵盖 Spring4.0 的所有核心内容:在 Eclipse 中安装 SpringIDE 插件、IOC & DI、在 Spring 中配置 Bean、自动装配、Bean 之间的关系(依赖、继承)、Bean 的作用域、使用外部属性文件、SpEL、管理 Bean 的...

    Spring高级培训:Spring高级培训的样本项目

    它涵盖了基础知识(例如BFPP和BPP,JSR和SpEL),提示(例如@Conditional,通知,Spring配置文件和代理),测试等。 卫星数据库 通过Spring Boot和HSQL在内存DB中使用JDBC。 周六 通过Spring Boot和H2在内存DB...

    SpringMVC暑期实训

    Eclipse搭建,包括代码和一些jar包,可能spring版本有些老,自传,下载,不喜勿喷 ...spring配置文件、Bean注入 3。spring表达式语言spEL 4。SpringData JPA关键技术 5。Spring IOC容器 6。Spring MVC容器

    xmljava系统源码-SpringInAction4:《SpringInAction4th》学习笔记

    xml java系统源码 SpringInAction4 《Spring In Action ...注解方式可以实现更细粒度的自动装配,Spring容器默认禁用注解装配,要在配置文件中开启 配置自动扫描 在基于Java的配置中使用@Configura

    Spring 入门到实战

    在Spring中配置Bean、自动装配、Bean之间的关系(依赖、继承)、Bean的作用域、使用外部属性文件、SpEL、管理Bean的生命周期、通过工厂方法配置Bean、通过注解配置Bean、注解方式AOP基础、前置通知、后置通知、返回...

    spring学习笔记

    ..............................................................................................................9 1.3.7Spring 框架加载配置文件:..............................................................

    企业后台管理基础框架 hsweb.zip

    通用mybatis配置文件,支持多种条件查询自动生成,支持自动生成insert,update,delete语句,支持和查询相同的各种条件.实现用户,权限管理;基于aop,注解,精确到按钮的权限控制.动态表单功能,可在前端设计表单,动态生成...

    从J2SE到J2EE知识点介绍

    2. 通过web.xml配置文件访问servlet的流程 129 (七) servlet里面的跳转和传参数的方法 130 1. 跳转 130 2. 传参数 131 (八) jsp中文乱码问题 131 1. JSP页面乱码 132 2. 表单提交中文时出现乱码 134 3. 关于jsp在...

    word源码java-hsweb-framework:forkfromhttps://github.com/hs-web,主要用于学习

    通用mybatis配置文件,支持多种条件查询自动生成,支持自动生成insert,update,delete语句 支持和查询相同的各种条件. 实现用户,权限管理;基于aop,注解,精确到按钮的权限控制. 动态表单功能,可在前端设计表单,动态生成...

Global site tag (gtag.js) - Google Analytics