美文网首页
AOP 之 HelloWorld

AOP 之 HelloWorld

作者: zlb | 来源:发表于2016-11-12 22:57 被阅读54次
    • 定义目标接口:
    package com.zlb.service;
    public interface IHelloWorldService {
        public void sayHello();
    }
    
    • 实现目标接口
    package com.zlb.service.imp;
    import com.zlb.service.IHelloWorldService;
    public class HelloWorldService implements IHelloWorldService{
        @Override
        public void sayHello() {
              System.out.println("============Hello World!");  
        }
    }
    
    • 定义切面支持类
    package com.zlb.aop;
    import org.aspectj.lang.ProceedingJoinPoint;
    public class HelloWorldAspect {
         //前置通知  
        public void beforeAdvice() {  
            System.out.println("===========before advice");  
        }  
    } 
    
    • XML配置
    <bean id="helloWorldService"   class="com.zlb.service.imp.HelloWorldService"/>  
    <bean id="aspect" class="com.zlb.aop.HelloWorldAspect"/>  
    <aop:config>  
        <aop:pointcut id="pointcut" expression="execution(* com.zlb.service.*.*(..))"/>  
        <aop:aspect ref="aspect">  
            <aop:before pointcut-ref="pointcut" method="beforeAdvice"/>  
        </aop:aspect>  
    </aop:config>
    
    • 运行结果
    ===========before advice
    ============Hello World!admin
    ===========after finally advice
    

    相关文章

      网友评论

          本文标题:AOP 之 HelloWorld

          本文链接:https://www.haomeiwen.com/subject/soiqpttx.html