• 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
    

  • 相关阅读:
    AxWindowsMediaPlayer的详细用法
    C# Windows Media Player控件的常用属性及方法
    C#中winform使用相对路径读取文件的方法
    DataGridView控件在加载数据时产生的闪烁问题
    浅谈C#中的双缓冲
    几种C#实现播放声音的方法
    C#下实现软件欢迎界面
    DataGridView控件内建立日期选择编辑列
    程序员值得看的电影(20170420EDIT)
    员工照片
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/4976957.html
Copyright © 2020-2023  润新知