Spring AOP通知传递参数时报错【 Constructor threw exception; nested exception is java.lang.IllegalArgu……】

82418c66f1093629.png

学习spring也有一段时间了,今天看到为通知传递参数时,突然出现了这个错误,昨天看环绕通知时还还不得了,所以便百度,google一通找,看到网上的解决方法有什么:缺少包啊、jdk版本与aspectj不一致啊等等等等,但仔细检查后发现并没有缺少哪个包,把jdk版本与aspectj弄成一致的还是依旧报错,这可就苦逼了,搞了一上午。。。。

抛出的错误为:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in file [D:\project\intellij\ceshi\src\main\java\spring-test.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: Lyric [Xlint:invalidAbsoluteTypeName]

spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="person" class="cn.zhouchenxi.test.entity.Person"
          p:name="xixi"
          p:age="22"
          p:sex="男"
          p:height="56.00"
          p:high="170.00"
    />
    <bean id="as" class="cn.zhouchenxi.test.entity.as"/>

    <aop:config>
        <aop:pointcut id="show" expression="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(String)) and args(Lyric)" />
        <aop:aspect ref="as">
            <aop:before pointcut-ref="show" method="goStage"/>
            <aop:after-returning pointcut-ref="show" method="end"  arg-names="Lyric"  />
            <aop:after-throwing pointcut-ref="show" method="over"/>
            <aop:around method="hahaha" pointcut-ref="show"/>
        </aop:aspect>
    </aop:config>
</beans>

我写的代码的目的是:aop的那个切点的expression值里的introduceSelf方法带了一个String参数Lyric,把它传到后置通知里。

<aop:pointcut id="show" expression="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(String)) and args(Lyric)" />
<aop:after-returning pointcut-ref="show" method="end"  arg-names="Lyric"  />

然后进过我仔细斟酌,我这里面还有前置通知,抛出异常的通知,环绕通知等等等等,这些都是没有参数的,会不会和这个有关?进过尝试,果然是这个原因,删除之后正常运行。配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="person" class="cn.zhouchenxi.test.entity.Person"
          p:name="希希"
          p:age="22"
          p:sex="男"
          p:height="56.00"
          p:high="170.00"
    />
    <bean id="as" class="cn.zhouchenxi.test.entity.as"/>

    <aop:config>
        <aop:pointcut id="show" expression="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(String)) and args(Lyric)" />
        <aop:aspect ref="as">
            <aop:after-returning pointcut-ref="show" method="end"  arg-names="Lyric"  />
        </aop:aspect>
    </aop:config>
</beans>

但是!如果我要在这个切点同时用前置通知,环绕通知等多种通知呢?进过尝试,so easy!只需要在定义一个<aop:config>就行了,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="person" class="cn.zhouchenxi.test.entity.Person"
          p:name="周晨希11"
          p:age="22"
          p:sex="男"
          p:height="56.00"
          p:high="170.00"
    />
    <bean id="as" class="cn.zhouchenxi.test.entity.as"/>

    <aop:config>
        <aop:pointcut id="show" expression="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(String)) and args(Lyric)" />
        <aop:aspect ref="as">
            <aop:after-returning pointcut-ref="show" method="end"  arg-names="Lyric"  />
        </aop:aspect>
    </aop:config>
    <aop:config>
        <aop:aspect ref="as">
            <aop:pointcut id="show1" expression="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(..))" />
            <aop:before pointcut-ref="show1" method="goStage"/>
            <aop:after-throwing pointcut-ref="show1" method="over"/>
            <aop:around method="hahaha" pointcut-ref="show1"/>
        </aop:aspect>
    </aop:config>


</beans>

注意的是:切点的id要再写一个,如上我写成了show1

或者不定义<aop:poincut>,直接在每个通知里写上完整的pointcut属性应该也可以,为了保证可行性,还是亲自试了一下,事实证明还是可行的!!

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="person" class="cn.zhouchenxi.test.entity.Person"
          p:name="希希"
          p:age="22"
          p:sex="男"
          p:height="56.00"
          p:high="170.00"
    />
    <bean id="as" class="cn.zhouchenxi.test.entity.as"/>
    <aop:config>
        <aop:aspect ref="as">
            <aop:before pointcut="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(..))" method="goStage"/>
            <aop:after-returning pointcut="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(String)) and args(Lyric)" method="end"  arg-names="Lyric"  />
            <aop:after-throwing pointcut="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(..))" method="over"/>
            <aop:around  pointcut="execution(* cn.zhouchenxi.test.entity.Person.introduceSelf(..))" method="hahaha"/>
        </aop:aspect>
    </aop:config>
</beans>


共有 2 条评论

  1. avatar

    admin

    评论一下

  2. avatar

    访客

    干得漂亮,博主真聪明!

Top