zoukankan      html  css  js  c++  java
  • spring aop ---基于AspectJ

    因为自己在引用jar包上吃过很多亏,也浪费过很多时间,所以每次都会把使用到的jar包都标明,谢谢理解!

    引用jar包:

    1、运行类:

    @Service
    public class Fit {
        public String say(){
            System.out.println("册数使用的数据!!!!");
    //        throw new RuntimeException("失败!!!");
            return "你好啊!!!";
        }
    }

    2、切面类:该类需@Component、@Aspect配合使用,因为classpath路径找不到@Aspect注释的类

    @Component
    @Aspect
    public class MyAspect {
        @Pointcut("execution(* aop.Fit.*(..))")
        public void pointcut(){};
        
        @Before("pointcut()")
        public void before(){
            System.out.println("before....");
        }
        
        @AfterReturning(pointcut = "pointcut()",returning="returnValue")
        public void afterReturnning(Object returnValue){
            System.out.println("afterReturnning....." + returnValue);
        }
        
        @AfterThrowing(pointcut="pointcut()",throwing="e")
        public void afterThrowing(RuntimeException e){
            System.out.println("throwing..." + e.getMessage());
        }
        
        @After("pointcut()")
        public void after(){
            System.out.println("after...");
        }
        
        @Around("pointcut()")
        public Object around(ProceedingJoinPoint pjp) throws Throwable{
            System.out.println("11111");
            Object obj = pjp.proceed();
            System.out.println("22222");
            return obj;
        }
    }

    3、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: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/aop 
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-4.0.xsd">
           
           <!-- 开启自动扫描 -->
           <context:component-scan base-package="aop"></context:component-scan>
           <!-- 开启自动代理、织入切面 -->
           <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>

    4、测试类:

    public class Testa {
    
        @Test
        public void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-aspectj.xml");
            Fit fit = (Fit)context.getBean("fit");
            fit.say();
        }
    }

    输出结果:

  • 相关阅读:
    javascript中事件
    pku 1836 Alignment
    pku 3086 Triangular Sums
    [转]asp格式化日期
    用数组作checkboxlist数据源
    (转)Membership、MembershipUser和Roles类 详解
    asp中判断 checkbox 是否选中
    使用 AddRange 方法将多个 ListItem 对象添加到集合
    My97DatePicker,一个兼容所有浏览器的日历选择脚本(相当经典)
    .Net下批量删除数据的存储过程问题(用动态SQL )
  • 原文地址:https://www.cnblogs.com/xl118/p/6793916.html
Copyright © 2011-2022 走看看