zoukankan      html  css  js  c++  java
  • java AOP使用配置项来进行注入实践

    场景:

    在目标方法前面和后面执行通知方法

    目标类

    @Component
    public class Play {
    
        public void watchTV(){
            System.out.println("目标执行方法,正在看电视");
        }
    }

    切面类

    public class WatchTVAspect {
    
        public void beforeAdvice(){
            System.out.println("切点函数执行前的方法");
        }
    
        public void afterAdvice(){
            System.out.println("切点函数执行后的方法");
        }
    
        public void afterReturning(){
            System.out.println("目标方法返回时执行 ,后置返回通知");
        }
    
        public void afterThrowing(){
            System.out.println("目标方法抛出异常时执行 异常通知");
        }
    
        public void around(){
            System.out.println("在目标函数执行中执行,可控制目标函数是否执行,环绕通知");
        }
    }

    applicationContext.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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <context:component-scan base-package="com.mb"></context:component-scan>
    
        <bean id="watchTv" class="com.mb.util.WatchTVAspect"></bean>
        <aop:config>
            <aop:aspect id="check" ref="watchTv">
                <aop:pointcut id="accountPoint" expression="execution(* com.mb.common.Play.*(..))"></aop:pointcut>
                <aop:before  pointcut-ref="accountPoint" method="beforeAdvice"></aop:before>
                <aop:after  pointcut-ref="accountPoint" method="afterAdvice"></aop:after>
                <!--<aop:around method="around" pointcut-ref="accountPoint" ></aop:around>-->
                <!--<aop:after-returning method="afterReturning" pointcut-ref="accountPoint"></aop:after-returning>-->
                <!--<aop:after-throwing method="afterThrowing" pointcut-ref="accountPoint"></aop:after-throwing>-->
            </aop:aspect>
        </aop:config>
    </beans>

    测试类

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath*:**/applicationContext*.xml"})
    public class DemoTest {
    
        @Autowired
        Play play;
    
        @Test
        public void aspectTest(){
            play.watchTV();
    
        }
    }

    测试结果:
    切点函数执行前的方法
    目标执行方法,正在看电视
    切点函数执行后的方法
  • 相关阅读:
    server正式的环境性能测试nginx-php 指着寻求突破的表现
    SICP 锻炼 (1.45)解决摘要
    sdut 3-4 长方形的周长和面积计算
    吉克1111-1114第七周讲座班、家庭作业(动态规划,期限:2014年4月25日本23点-周五晚上,科委飞信通知学生)
    STL源代码分析——STL算法sort排序算法
    伺服驱动器UVW电机电源线相序错误
    1_BLE nRF51822 UART 与 BLE转发
    研制埃博拉疫苗与科学家的奇思秒想
    垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想
    Recover Binary Search Tree -- LeetCode
  • 原文地址:https://www.cnblogs.com/unknows/p/10316049.html
Copyright © 2011-2022 走看看