springAOP模板
2015-08-02 11:49
549 查看
定义一个业务,对其进行aop开发,这里使用计算机为案例:
开发目标代码:
开发aop类:
在aop类中需要把该类标识为组件,让spirng容器进行管理,在有就是标识为切面,spring容器可以代理生成对象。
开发spring的核心容器:
测试代码:
开发目标代码:
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后置通知 }
相关文章推荐
- java多线程5--资源访问权限
- Java之集合类【HashSet】【入门版,实例解析】
- Java之集合类【ArrayList】【入门版,实例解析】
- Java构造方法
- springmvc 的数据回显
- Java之反射机制【入门版(4),实例解析】
- JAVA之Class类和getClass
- JavaBean学习--练习示例
- 【Spring Security】之一:Eclipse搭建Spring Security项目
- 归并排序 java语言实现
- Spring的自动装配
- Java之集合类【HashMap】【入门版,实例解析】
- javax.servlet.ServletException: bean [name] not found within scope
- Java之反射机制【入门版(4)】
- JavaWeb 分页管理
- Java之反射机制【入门版(3)】
- 异常:java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
- Java之反射机制【入门版(1)】
- java学习笔记之-构造函数
- Java之克隆