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

  • 相关阅读:
    POJ-1088 滑雪 (包含部分自用测试数据)
    PHP-从零开始使用Solr搜索引擎服务(下)
    PHP-从零开始使用Solr搜索引擎服务(上)
    15位身份证号转化为18位身份证号
    php的数组转为对象
    H5页面遮罩弹框下层还能滚动的问题
    纯css实现长宽等比例的div
    VUE开发一个图片轮播的组件
    jQuery map和each用法
    表格式布局让每个列高度等于该行最大高度
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7234773.html
Copyright © 2011-2022 走看看