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.运行结果如下:

  • 相关阅读:
    [Redis]主从同步可能遇到的坑
    Redis_如何保证原子操作
    .Net Core 5.0 Json序列化和反序列化 | System.Text.Json 的json序列化和反序列化
    JavaScript Error对象整理_JavaScript 异常处理整理
    Canvas 事件绑定|Canvas事件处理
    Css3 常用布局方式 一行两列&高度自适应&垂直方向居中
    Css3 实现锯齿效果整理
    Css3 currentColor 变量使用
    Css3 实现任意角扇形|Css3实现六角扇形
    实现 Application_Start 和 Application_End
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7234773.html
Copyright © 2011-2022 走看看