• spring-aop示例


     具体案例放在github上,主要是jar包在上面

    https://github.com/guoyansi/spring-aop-example

    knights.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:p="http://www.springframework.org/schema/p" 
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:mvc="http://www.springframework.org/schema/mvc" 
            xmlns:tx="http://www.springframework.org/schema/tx" 
            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/context 
            http://www.springframework.org/schema/context/spring-context.xsd 
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
    
            <bean id="knight" class="p1.BraveKnight">
                <constructor-arg ref="quest" />
            </bean>
            <bean id="quest" class="p1.SlayDragonQuest"></bean>
            
            <bean id="minstrel" class="p1.Minstrel"></bean>
            
            <aop:config>
                <aop:aspect ref="minstrel">
                    <aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))" />
                    <aop:before pointcut-ref="embark" method="singBeforeQuest"/>
                    <aop:after pointcut-ref="embark" method="singAfterQuest"/>
                </aop:aspect>
            </aop:config>              
    </beans>

    IQuest.java

    package p1;
    
    /**
     * 探险
     *
     */
    public interface IQuest {
        void embark();
    }

    IKnight.java

    package p1;
    
    public interface IKnight {
        void embarkOnQuest();
    }

    BraveKnight.java

    package p1;
    
    /**
     * 骑士
     *
     */
    public class BraveKnight implements IKnight{
        private IQuest quest;
        
        public BraveKnight(IQuest quest){
            this.quest=quest;
            
        }
        @Override
        public void embarkOnQuest (){
            quest.embark();
        }
        
    }

    SlayDragonQuest.java

    package p1;
    
    public class SlayDragonQuest implements IQuest{
        @Override
        public void embark() {
            System.out.println("SlayDragonQuest的embark......");        
        }
    }

    Minstrel.java

    package p1;
    
    public class Minstrel {
        public void singBeforeQuest(){
            System.out.println("探险之前...");
        }
        public void singAfterQuest(){
            System.out.println("探险之后......");
        }
        
    }
    package p1;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Run {
        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("classpath*:knights.xml");
            IKnight knight=(IKnight)context.getBean("knight");
            knight.embarkOnQuest();
        }
    }

    执行run的结果:

    上面是一个完整的例子.如果没有commons-logging.jar

    控制台上的那些红色字样就不会输出,还会出现异常.

    如果没有aopalliance.jar:           

    nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

    如果没有aspectjweaver.jar

    nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
    

  • 相关阅读:
    js模态框实现原理
    静态库、动态库------深入理解计算机系统
    链接器如何解析多重定义的全局符号(强弱符号)------深入理解计算机系统
    linux------深入理解linux内核
    libcurl坑
    《将博客搬至CSDN》
    openssl 编译
    vs2015+opencv3.3.1+ c++实现 静态背景下多运动目标提取,检测
    QT 相关书籍
    qml 知识积累
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/4976957.html
Copyright © 2020-2023  润新知