spring框架 aop

 1 注解部分:
 2 <?xml version="1.0" encoding="UTF-8"?>
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
 9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
10 
11     <bean id="applepay" class="com.zhidi.bean.ApplePay"/>
12     <bean id="mypay" class="com.zhidi.bean.MYPay"/>
13     <!--  -->
14     <bean id="advice" class="com.zhidi.bean.Advice"/>
15     
16     <!--注解配置  -->
17     <context:component-scan base-package="com.zhidi"/>
18     <aop:aspectj-autoproxy/>
19             <!--xml配置  -->    
20 <!--     <aop:config>
21         创建切入点 
22         <aop:pointcut expression="execution( * com.zhidi.bean..*.*(..))" id="pointcut"/>
23         创建切入面
24         <aop:aspect  ref="advice">
25         前置通知
26         <aop:before method="before" pointcut-ref="pointcut"/>
27         
28         后置返回通知
29         <aop:after-returning method="after" pointcut-ref="pointcut"/>
30         <aop:after method="afterlog" pointcut="execution( * com.zhidi.bean..*.*(..)) and args(userId,money)" arg-names="userId,money"/>
31         
32         <aop:before method="beforelog" pointcut="execution( * com.zhidi.bean..*.*(..)) and args(userId,money)" arg-names="userId,money"/>
33         
34         抛出异常后通知
35         <aop:after-throwing method="throwing" pointcut-ref="pointcut" throwing="e"/>
36         环绕通知
37         <aop:around method="aroundAdvice" pointcut-ref="pointcut"/>
38         </aop:aspect>
39     
40     </aop:config> -->
41 </beans>
通知类:package com.zhidi.bean;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;

public class Advice {
    public void before(){
        System.out.println("准备支付!!!!");
        
    }
    public void after(){
        System.out.println("支付完成!!");
        
    }
    public void beforelog(Integer userId, double money){
        String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        System.out.println("当前时间:"+ now +"用户ID:"+userId+"准备支付");
        
        
    }
    public void afterlog(Integer userId, double money){
        String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        System.out.println("当前时间:"+ now +"用户ID:"+userId+"支付成功,支付金额为:" + money);
    }
    public void throwing(Exception e){
        System.out.println("代码出现异常,异常为----"+e.getMessage());
        
        
    }
    public void afterFinally(){
        System.out.println("必须执行!!");
    }
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("环绕前");
        Object result = joinPoint.proceed();
        System.out.println("环绕后");
        return result;
    }
}
抽象类接口:
package com.zhidi.interf;
/**
 * 支付接口
 * @author Administrator
 *
 */
public interface PayService {
    //接口支付方法
    public boolean pay(Integer userId,double money);

}
目标类1:

package com.zhidi.bean;


import com.zhidi.interf.PayService;


public class MYPay implements PayService {
public boolean pay(Integer userId, double money) {
System.out.println("用户id:"+ userId + "使用支付宝支付方式支付金额:"+ money );
return true;
};
}

目标类2:

package com.zhidi.bean;


import com.zhidi.interf.PayService;


public class ApplePay implements PayService {


@Override
public boolean pay(Integer userId, double money) {
System.out.println("用户id:"+ userId + "使用苹果支付方式支付金额:"+ money);
return false;
}


}

测试类:

package com.zhidi.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.zhidi.bean.ApplePay;
import com.zhidi.interf.PayService;


public class Test02 {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
PayService ap= (PayService) app.getBean("applepay",PayService.class);
ap.pay(1, 100);
}
}

 

 

posted @ 2017-08-30 20:11  疯子++  阅读(293)  评论(0)    收藏  举报