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

  • 相关阅读:
    第13章 使用ADO.NET访问数据库
    第11章 连接查询和分组查询
    第10章 模糊查询和聚合函数
    第9章 数据查询基础
    数据库前三章测试题
    用表组织数据
    程序数据集散地:数据库
    深入C#的String类
    线程池
    hadoop-2.8.0 完全分布式运行模式
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7234773.html
Copyright © 2020-2023  润新知