spring基础学习---aop

1:无参aop下面为项目结构

2:通知类。MyAdvice

package cn.edu.aop;

import org.aspectj.lang.ProceedingJoinPoint;

//通知类
public class MyAdvice {
    //前置通知
    public void before(){
        System.out.println("before...");
    }
    //后置通知
    public void after(){
        System.out.println("after...");
    }
    //返回后通知
    public void afterReturning(){
        System.out.println("afterReturning...");
    }
    //抛出异常后通知
    public void afterThrowing(){
        System.out.println("afterThrowing...");
    }
    //环绕通知//特殊
    public void around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("around before...");
        //执行切入点方法
        pjp.proceed();
        System.out.println("around after...");
    }
}

3:服务类。UserService

package cn.edu.aop;

public class UserService {
    // 切入点
    public void add() {
        System.out.println("add");
        // 制造出现异常
        // int x = 1/0;
    }
}

4:测试类。AOPAPP 

package cn.edu.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPAPP {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService us = (UserService) ctx.getBean("userService");
        us.add();
    }

}

5:配置文件。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"
    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.xsd">
<!-- 通知类 -->
    <bean id="myAdvice" class="cn.edu.aop.MyAdvice"></bean>
    <aop:config>
        <aop:aspect ref="myAdvice">
            <aop:pointcut expression="execution(* *..*.add())" id="pt" />
            <!-- 各种切入类型 -->
            <!--  
            <aop:before method="before" pointcut-ref="pt"  />
                <aop:after method="after" pointcut-ref="pt"/> 
                    <aop:after-returning method="afterReturning" pointcut-ref="pt"/> 
        
            <aop:after-throwing method="afterThrowing" pointcut-ref="pt"/>
            <aop:around method="around" pointcut-ref="pt"/> -->

        </aop:aspect>
    </aop:config>

    <bean id="userService" class="cn.edu.aop.UserService"></bean>
</beans>

--------------------------------------------------以上为无参AOP----------------------------------------------

--------------------------------------------------以下为有参AOP----------------------------------------------

//通知类
public class MyAdvice {
    // 前置通知
    public void before(JoinPoint jp) {
        System.out.println("before...");
        // 获取切入点方法参数
        Object[] objs = jp.getArgs();
        System.out.println("切入点方法参数:" + objs[0] + "," + objs[1]);
    }

    // 后置通知
    public void after(JoinPoint jp) {
        System.out.println("after...");
    }

    // 返回后通知
    public void afterReturning(JoinPoint jp, Object asd) {
        System.out.println("afterReturning...");
        // 输出切入点方法的返回值
        System.out.println(asd+"123");
    }

    // 抛出异常后通知
    public void afterThrowing() {
        System.out.println("afterThrowing...");
    }

    // 环绕通知
     public Object around(ProceedingJoinPoint pjp) throws Throwable{
     System.out.println("around before...");
     //获取切入点方法参数
     Object[] objs = pjp.getArgs();
     //去掉输入字符串参数前后的空格
     objs[0] = objs[0].toString().trim();
     System.out.println("切入点方法参数:"+objs[0]+","+objs[1]);
     //执行切入点方法
     Object result = pjp.proceed(objs);
     //输出切入点方法的返回值
     System.out.println(result);
     System.out.println("around after...");
     return 200;
     }
    
}
<?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"
    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.xsd">
    <bean id="myAdvice" class="cn.edu.aop.MyAdvice"></bean>
    <aop:config>
        <aop:aspect ref="myAdvice">
            <aop:pointcut expression="execution(* *..*.add(..))" id="pt"/>
            <!--  <aop:before method="before" pointcut-ref="pt"/>  -->
             <aop:after-returning method="afterReturning" pointcut-ref="pt" returning="asd"/> 
        <!--     <aop:around method="around" pointcut-ref="pt"/>  -->
        </aop:aspect>
    </aop:config>
    <bean id="userService" class="cn.edu.aop.UserService"></bean>
</beans>
public class UserService {

    public int add(String a, int b) {
        System.err.println("add");
        System.out.println("add方法的输入参数" + a + "," + b);
        return 2017;
    }

}
public class AOPAPP {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService us = (UserService) ctx.getBean("userService");
    int r=us.add("wowokkk", 100);
    System.out.println("add方法返回值APP"+r);
    }

}

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

posted @ 2017-04-19 20:01  开文博客园  阅读(308)  评论(0编辑  收藏  举报