Spring使用——切面编程AOP使用

  在我们的spring xml配置中,加上<aop:config>之后,Eclipse报错,提示The prefix "aop" for element "aop:config" is not bound,这是因为,需要在beans中加入xmlns:aop="http://www.springframework.org/schema/aop",以及在xsi:schemaLocation中加入http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd然后问题就可以解决了。

  再运行程序,报错Unexpected exception parsing XML document from class path resource [com/juggler/juggler.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice。这是因为缺少了一个aopalliance.jar,下载了一个aopalliance-1.0.jar之后。

  运行后报bean创建异常了异常信息是这样的:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'juggler22' defined in class path resource [com/juggler/juggler.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':

Cannot create inner bean '(inner bean)#5f0ab09f' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument;

nested exception is org.springframework.beans.factory.BeanCreationException:

Error creating bean with name '(inner bean)#5f0ab09f': Cannot create inner bean '(inner bean)#6c69d02b' of type [org.springframework.aop.aspectj.AspectJExpressionPointcut] while setting constructor argument;

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#6c69d02b':

Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

  错误消息很长,意思就是在创建我们自己指定的juggler bean的时候,内部对象发生错误了。一开始以为是自己的juggler bean定义的时候有问题,不过之前也是这么定义的,所以应该没问题,删除掉aop的config部分之后,程序也能正常运行。那么就是string aop部分引入的问题了,google后发现,其实又是少了aspectj-1.6.9.jar包,还有aspectjweaver-1.6.9.jar包。因为spring的aop是使用了aspectj的部分功能的,所以需要引入aspectj的jar包也是正常。

  不过这里不得不吐槽一下java的第三方包库。。。真的有点环环相扣的感觉了,部署环境比较恶心。

  不过这个问题解决了之后,我的第一次spring aop的简单测试,也是顺利的输出预计的结果了。

  其中Juggler类:

package com.juggler;

public class Juggler {
    private int beanBags = 0;
    private Poem poem = null;
    private Instructment instructment;
    
    public Juggler(int _beanBags, Poem _poem) {
        System.out.println("Juggler constructor");
        beanBags = _beanBags;
        poem = _poem;
    }
    
    public void perform() {
        System.out.println("throw " + Integer.toString(beanBags) + " _beanBags");
        poem.read();
        instructment.play();
    }
    
    public void initObject() {
        System.out.println("juggler initObject calls");
    }
    
    public void destroyObject() {
        System.out.println("juggler destroyObject calls");
    }
    
    public void setInstructment(Instructment _instructment) {
        instructment = _instructment;
    }
}

  Audience类代码:

package com.juggler;

public class Audience {
    public Audience() {
        
    }
    public void takeSeat() {
        System.out.println("audience take seat");
    }
    
    public void applaud() {
        System.out.println("clap clap clap");
    }
}

  juggler.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       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/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd"
       default-init-method = "initObject"
       default-destroy-method = "destroyObject">
    
    <bean id="juggler" class="com.juggler.Juggler">
        <constructor-arg value="5" />
        <constructor-arg ref="sonnet" />
        <property name="instructment" ref="piano" />
    </bean>
    
    <bean id="sonnet" class="com.juggler.SonnetPoem"/>
    <bean id="sonnetMulti" class="com.juggler.SonnetPoem" scope="prototype"/>
    
    <bean id="piano" class="com.juggler.Piano" />
    <bean id="audience" class="com.juggler.Audience" />
    
    <aop:config>
        <aop:aspect ref="audience">
            <aop:before pointcut="execution(* com.juggler.Juggler.perform(..))" method="takeSeat" />
            <aop:after  pointcut="execution(* com.juggler.Juggler.perform(..))" method="applaud" />
        </aop:aspect>
    </aop:config>
</beans>

  上述代码,我们有几个类的代码就没有贴出来了,只是简单的测试一下aop的切片功能。

  从xml中我们可以了解,主要是针对juggler的perform做了一个切片,在它执行前,执行后分别调用了我们指定的method。

原文地址:https://www.cnblogs.com/chobits/p/5130443.html