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

  • 相关阅读:
    自己动手编写一个网络图片爬虫
    使用maven构建项目的注意事项
    maven 构建一个web项目
    构建简单的Maven工程,使用测试驱动的方式开发项目
    像Maven一样构建java项目的目录,更好的管理java工程的源码
    Tomcat源码导入eclipse的步骤
    [HNOI2012] 矿场搭建
    UVA10641 Barisal Stadium 照亮体育馆
    CSP-J/S(NOIP PJ/TG) 游记
    [BalticOI 2011 Day1] Switch the Lamp On
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7234773.html
Copyright © 2011-2022 走看看