zoukankan      html  css  js  c++  java
  • spring aop简单实现

    1.导入额外的两个jar包:

    2.编写接口:Helloworld

    package com.xuzhiwen.test5;
    
    public interface Helloworld {
        public void printHelloworld();
        public void printGoodBye();
    }

    3.编写类实现接口:HelloworldImpl1

    package com.xuzhiwen.test5;
    
    public class HelloworldImpl1 implements Helloworld{
    
        @Override
        public void printHelloworld() {
            System.out.println("HelloworldImpl1.printHelloworld");
        }
    
        @Override
        public void printGoodBye() {
            System.out.println("HelloworldImpl1.printGoodBye");
        }
    }

    4.编写类实现接口:HelloworldImpl2

    package com.xuzhiwen.test5;
    
    public class HelloworldImpl2 implements Helloworld{
    
        @Override
        public void printHelloworld() {
            System.out.println("HelloworldImpl2.printHelloworld");
        }
    
        @Override
        public void printGoodBye() {
            System.out.println("HelloworldImpl2.printGoodBye");
        }
    
    }

    5.编写日子类:TimeHandler

    package com.xuzhiwen.test5;
    
    public class TimeHandler {
        public void printTime(){
            System.out.println("time = " + System.currentTimeMillis());
        }
    }

    6.编写配置文件:aop.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        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-4.3.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-4.3.xsd">
            
            <bean id="helloworldImpl1" class="com.xuzhiwen.test5.HelloworldImpl1" />
            <bean id="helloworldImpl2" class="com.xuzhiwen.test5.HelloworldImpl2" />
            <bean id="timeHandler" class="com.xuzhiwen.test5.TimeHandler" />
            
            <aop:config>
                <aop:aspect id="time" ref="timeHandler">
                    <aop:pointcut id="addAllMethod" expression="execution(* com.xuzhiwen.test5.*.*(..))" />
                    <aop:before method="printTime" pointcut-ref="addAllMethod" />
                    <aop:after method="printTime" pointcut-ref="addAllMethod" />
                </aop:aspect>
            </aop:config>
    </beans>

    7.编写测试类:Test

    package com.xuzhiwen.test5;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        
        @org.junit.Test
        public void test(){
            ApplicationContext app = new ClassPathXmlApplicationContext("aop.xml");
            Helloworld hello1 = (Helloworld) app.getBean("helloworldImpl1");
            hello1.printHelloworld();
            hello1.printGoodBye();
            System.out.println("--------------------------------------------");
            Helloworld hello2 = (Helloworld) app.getBean("helloworldImpl2");
            hello2.printHelloworld();
            hello2.printGoodBye();
        }
    }

    8.运行结果如下:

  • 相关阅读:
    Golang gRPC学习(01): gRPC介绍
    MySQL InnoDB存储引擎大观
    SpringBoot学习-图文并茂写Hello World
    Golang的goroutine协程和channel通道
    业务 产品 技术的一点看法
    需求一直做不完,怎么办?
    技术管理:项目开发中的几种风险管理
    go内存管理
    etcd实现分布式锁分析
    强缓存与协商缓存
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7234773.html
Copyright © 2011-2022 走看看