您的位置:首页 > 编程语言 > Java开发

springAOP模板

2015-08-02 11:49 549 查看
定义一个业务,对其进行aop开发,这里使用计算机为案例:

开发目标代码:

package com.Calculator;

import org.springframework.stereotype.Component;
@Component(value = "Calculator")
public class CalculatorImpl implements ICalculator {
public int add(int i, int j) {
int result = i + j;
return result;
}

public int sub(int i, int j) {
int result = i - j;
return result;
}

public int mul(int i, int j) {
int result = i * j;
return result;
}

public int div(int i, int j) {
int result = i / j;
return result;
}
}


开发aop类:

在aop类中需要把该类标识为组件,让spirng容器进行管理,在有就是标识为切面,spring容器可以代理生成对象。

package com.Calculator;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
@Aspect
public class CalculatroAop {
/*
* execution的格式(限定范围 + 返回类型 +全限类名 +方法名(参数类型。。。))
* 可以用* 代替限定范围 + 返回类型,方法名
* 可以用英文句号代替参数类型,任意的参数都可以匹配,几个参数就几个句号,中间空格隔开
* */

@Before("execution(* com.jd.jr.Calculator.CalculatorImpl.*(. .))")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("method "+"{"+methodName+"}"+"  begin...with"+args);

}

@After("execution(* com.jd.jr.Calculator.CalculatorImpl.*(. .))")
public void aftereMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("method "+"{"+methodName+"}"+"  ends...");

}

/*
* 在返回通知中,可以通过在注解参数列表中添加returnint = "返回结果保存的变量"
* 在通知方法的参数中添加一个参数就是 “返回结果保存的变量”,可以得到结果
* */
@AfterReturning(value = "execution(* com.jd.jr.Calculator.CalculatorImpl.*(. .))"
, returning = "result"
)
public void returnMethod(JoinPoint joinPoint,Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("method "+"{"+methodName+"}"+"  ends with result "+result);
}

/*
* 在异常通知中,可以得到异常的信息,方法和返回通知的类似
* */
@AfterThrowing(value = "execution(* com.jd.jr.Calculator.CalculatorImpl.*(. .))"
,throwing = "e"
)
public void afterThrowing(JoinPoint joinPoint,Exception e){
String methodName = joinPoint.getSignature().getName();
System.out.println("method "+"{"+methodName+"}"+"  ends with exception "+e);
}

}

开发spring的核心容器:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!--配置自动扫描的包的路径-->
<context:component-scan base-package="com.jd.jr.Calculator"/>
<!--使用aspject注解的方式自动生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>


测试代码:

public class MainTes {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
ICalculator calculator =cxt.getBean(ICalculator.class);
int result1 = calculator.add(1, 2);
System.out.println("result1 is :"+result1);

int result2 = calculator.div(88, 0);
System.out.println("result2 is "+result2);

int result3 = calculator.mul(2, 4);
System.out.println("result3 is "+result3);

int result4 = calculator.sub(5,3);
System.out.println("result4 is "+result4);

}
}


try{
@Before前置通知
主代码块
@AfterReturing返回通知(可以访问到代码执行结果)
}catch(Exception e){
@throwing 异常通知
}
finally{
@After后置通知
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: