aj通知函数
MyAspectj中的代码:
package com.gaojian.aspectj.test;
public aspect MyAspectj {
??? /*调用某个文件夹下面所有的class*/
??? //??? pointcut callF(): execution(* com.gaojian..*.*(..));
???
??? /**
??? ?* 1) before : 在目标函数执行前执行
??? ?* 2) after :在目标函数执行后执行
??? ?* 3) after returning : 在目标函数返回是执行
??? ?* 4) after throwing : 在目标函数抛出异常时执行
??? ?* 5) around : 在目标函数执行中执行
??? ?*/
??? pointcut callF(int begin) : execution(!static * com.gaojian.aspectj.test1.Function.*(int, ..)) && args(begin,..);
???
//??? before(int begin) : callF(begin) {
//??? ??? System.out.println("*****************before*****************");
//??? }
???
//??? after(int begin) : callF(begin) {
//??? ??? System.out.println("*****************after*****************");
//??? }
???
//??? after(int begin) returning(int result): callF(begin) {
//??? ??? System.out.println("****************after return******************");
//??? ??? // 返回结果的输出
//??? ??? System.out.println(result);
//??? }
???
//??? after(int begin) throwing(Exception e) : callF(begin) {
//??? ??? System.out.println("抛出异常的类型:" + e.getClass().getSimpleName());
//??? ??? System.out.println("抛出异常的内容: " + e.getMessage());
//??? ??? System.out.println(e.toString());
//??? ??? System.out.println(thisJoinPoint.getSourceLocation());
//????? System.out.println(thisJoinPoint.getStaticPart());
//??? }
???
//??? Object around(int begin) : callF(begin) {
//??? ??? if(begin == 0) {
//??? ??? ??? begin = 1;
//??? ??? }
//??? ??? Object result = proceed(begin);
//??? ??? return result;
//??? }
}
?
Function中的代码
package com.gaojian.aspectj.test1;
public class Function {
??? public void f(int begin, int end) {
??? ??? System.out.println("method execution!");
??? }
???
??? public int f1(int begin) {
??? ??? System.out.println("method execution");
??? ??? return begin;
??? }
???
???
??? public void f2(int begin) {
??? ??? System.out.println("method execution");
??? ??? if(begin == 0) {
??? ??? ??? throw new RuntimeException("begin can not be '0'!");
??? ??? }
??? }
???
??? public static void main(String[] args){
??? ??? Function function = new Function();
??? ??? /* 没有返回结果的函数执行 */
//??? ??? function.f(0, 1);
??? ??? /* 有返回结果的函数执行 */
//??? ??? function.f1(0);
??? ??? /* 有异常抛出 */
//??? ??? function.f2(0);
??? ??? /* around的使用 */
??? ??? /**
??? ??? ?* 输出的结果应该为0,经过切面中环绕函数的处理,结果变为了1。
??? ??? ?*/
//??? ??? System.out.println(function.f1(0));
??? }
}
?