src\dayday\Count.java

package dayday;

import org.springframework.stereotype.Component;

/**
* Created by I am master on 2016/12/3.
*/
@Component
public interface Count {
int add(int i, int j);
int mul(int i, int j);
int div(int i, int j);
int sub(int i, int j);
}

src\dayday\CountImpl.java

package dayday;

import dayday.Count;
import org.springframework.stereotype.Component; /**
* Created by I am master on 2016/12/3.
*/
@Component
public class CountImpl implements Count {
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;
}
}

src\dayday\LogginAspect.java

package dayday;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import java.util.Arrays;
import java.util.List; /**
* Created by I am master on 2016/12/3.
*/
@Aspect
@Component
public class LogginAspect {
@Before("execution(public int dayday.Count.add(int,int))")
public void beforeMethod(JoinPoint joinpoint){
String methodName=joinpoint.getSignature().getName();
List<Object>args= Arrays.asList(joinpoint.getArgs());
System.out.println("method is "+methodName+" begins with "+args);
}
@After("execution(public int dayday.Count.add(int,int))")
public void afterMethod(){
System.out.println("After...");
}
@AfterReturning(value="execution(public int dayday.Count.add(int,int))",returning = "result")
public void afterReturning(JoinPoint joinpoint,Object result){
String methodName=joinpoint.getSignature().getName();
System.out.println("method is "+methodName+" result is "+result);
}
@AfterThrowing(value="execution(public int dayday.Count.div(int,int))",throwing ="exception")
public void afterThrowing(JoinPoint joinpoint,Exception exception){
String methodName=joinpoint.getSignature().getName();
System.out.println("method is "+methodName+" occurs "+exception);
}
@Around("execution(public int dayday.Count.add(int,int))")
public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null;
String methodName = pjd.getSignature().getName(); try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
//执行目标方法
result = pjd.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
//异常通知
System.out.println("The method " + methodName + " occurs exception:" + e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
}

src\dayday\Main.java

package dayday;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by I am master on 2016/12/3.
*/
public class Main {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Count c = ctx.getBean(Count.class);
int result=c.add(8,3);
}
}

src\beans.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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="dayday"/>
<!--使AspectJ注释起作用,自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy/>
</beans> 基于xml配置切面
src\dayday\beans-xml.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-->
<bean id="countimpl" class="dayday.CountImpl"></bean> <!--配置切面的Bean-->
<bean id="logginaspect" class="dayday.LogginAspect"></bean> <!--配置AOP-->
<aop:config>
<!--配置切点的bean-->
<aop:pointcut expression="execution(* dayday.Count.*(int, int))" id="pointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="logginaspect">
<aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
<aop:after method="afterMethod" pointcut-ref="pointcut"></aop:after>
<aop:after-returning method="afterReturning" returning="result" pointcut-ref="pointcut"></aop:after-returning>
<aop:after-throwing method="afterThrowing" throwing="exception" pointcut-ref="pointcut"></aop:after-throwing>
</aop:aspect>
</aop:config>
</beans>
 

初学AOP的更多相关文章

  1. 初学AOP小结

    Spring AOP理解 参考链接 AOP简介 AOP(面向切面编程),可以说时OOP的补充,使用OOP时,我们在日常编写代码的时候,一旦牵涉到大型一点的项目,项目不可或缺的事务处理,安全处理,验证处 ...

  2. spring boot 从开发到上线(三)—AOP 异常监控、上报

    在做这个项目的期间,看到一篇很有启发性的文章<程序员你为什么这么累>.对于初级程序员来说,拿到需求,第一反应是用什么技术来尽快的完成任务,这本身并没有问题.但长此以往,不仅被需求的更改搞得 ...

  3. Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

    实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...

  4. Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。

    实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

  5. Spring初学之annotation实现AOP前置通知和后置通知

    实现两个整数的加减乘除,并在每个计算前后打印出日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

  6. 【spring boot】SpringBoot初学(6)– aop与自定义注解

    前言 github: https://github.com/vergilyn/SpringBootDemo 一.AOP 官方demo:https://github.com/spring-project ...

  7. 【初学Java学习笔记】AOP与OOP

    AOP(Aspect Oriented Programming) 面向切面编程,是属于Spring框架中的内容.AOP相当于OOP的补充,当我们需要对多个对象引入一个公共行为,比如日志,操作记录等,就 ...

  8. 初学源码之——银行案例手写IOC和AOP

    手写实现lOC和AOP 上一部分我们理解了loC和AOP思想,我们先不考虑Spring是如何实现这两个思想的,此处准备了一个『银行转账」的案例,请分析该案例在代码层次有什么问题?分析之后使用我们已有知 ...

  9. .Net中的AOP读书笔记系列之AOP介绍

    返回<.Net中的AOP>系列学习总目录 本篇目录 AOP是什么? Hello,World! 小结 本系列的源码本人已托管于Coding上:点击查看,想要注册Coding的可以点击该连接注 ...

随机推荐

  1. poll()函数的使用

    分类: LINUX poll函数用于监测多个等待事件,若事件未发生,进程睡眠,放弃CPU控制权,若监测的任何一个事件发生,poll将唤醒睡眠的进程,并判断是什么等待事件发生,执行相应的操作.poll函 ...

  2. java网络编程,简单的客户端和服务器端

    1.服务器端 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import ...

  3. CSS3 选择器——基本选择器

    CSS的选择器,我想大家并不会陌生吧,因为天天在使用,但对于CSS3的选择器,要运用的灵活到位,我想对很多朋友还是一定的难度,特别是CSS3中的:nth选择器.那么从现在开始我们先丢开他们版本的区别, ...

  4. 【JAVA】【leetcode】【使用堆栈实现后向计算】

    题目:evaluate-reverse-polish-notation 要求: Evaluate the value of an arithmetic expression in Reverse Po ...

  5. Web之路笔记之一

    简单说一句,现在开始准备面试前端的知识,每天完成相关的任务,记录一些点. 2014秋季学期Web2.0课程习题 <Lab1 - About Me Page> 目标是自己动手写一个粗略的包含 ...

  6. HTML页面优化

    第一步:加载优化 减少HTTP请求. 因为手机浏览器同时响应请求为4个请求(Android支持4个,iOS 5后可支持6个),所以要尽量减少页面的请求数,首次加载同时请求数不能超过4个.a) 合并CS ...

  7. Javascript学习笔记2.1 Javascript与DOM简介

    DOM(文档对象模型)简介 DOM(文档对象模型)针对HTML和XML文档的一个API. DOM可以将任何HTML或XML文档描绘成由多层节点构成的树形结构,它是中立于平台和语言的接口,允许程序和脚本 ...

  8. 个人介绍和Github使用流程

    我叫石莉静,来自网络工程143班,学号1413042067 我的兴趣爱好有看电影.动漫,听音乐,摄影,寻找美食等等. 个人编程能力:非常真诚的说,我的编程能力蛮差的,用C++写过一共写过...(很少很 ...

  9. 十天学会DIV+CSS(DIV布局)

    一列布局: 一列固定宽度.一列固定宽度居中.一列自适应宽度.一列自适应宽度居中 一列固定宽度 <head> <style type="text/css"> ...

  10. 计算机网络(9)-----TCP可靠传输的实现

    TCP可靠传输的实现 以字节为单位的滑动窗口 滑动窗口的滑动是以字节为单位的,发送方A和接收方B在TCP三次握手的前两次握手时协商好了发送窗口和接受窗口的大小,发送方A根据B发送来的确认连接报文中标明 ...