码迷,mamicode.com
首页 > 编程语言 > 详细

spring-aop示例

时间:2015-11-19 12:40:47      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

 具体案例放在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

技术分享

 

spring-aop示例

标签:

原文地址:http://www.cnblogs.com/guoyansi19900907/p/4976957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!