一、AOP简介:

二、AOP实例:

三、使用的例子

需求:在student添加的前后,打印日志信息;

0)spring AOP需要引用的jar包:

1)StudentService.java接口:

package com.cy.service;

public interface StudentService {
public void addStudent(String name);
}

2)StudentServiceImpl.java实现类:

package com.cy.service.impl;

import com.cy.service.StudentService;

public class StudentServiceImpl implements StudentService {

    @Override
public void addStudent(String name) {
System.out.println("添加学生"+name);
} }

3)StudentServiceAspect.java切面类:

package com.cy.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { //前置通知,方法执行之前
public void doBefore(JoinPoint jp){
System.out.println("类名:" + jp.getTarget().getClass().getName());
System.out.println("方法名:" + jp.getSignature().getName());
System.out.println("开始添加学生:" + jp.getArgs()[0]);
} //后置通知 方法完成之后
public void doAfter(JoinPoint jp){
System.out.println("类名:" + jp.getTarget().getClass().getName());
System.out.println("方法名:" + jp.getSignature().getName());
System.out.println("完成添加学生:" + jp.getArgs()[0]);
} //环绕通知 可以在业务方法的前后添加逻辑
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal = pjp.proceed(); //retVal为StudentServiceImpl.addStudent(String name)方法的返回值
//因为返回值为void,所以console打印null
System.out.println("添加学生后");
System.out.println(retVal);
return retVal;
} //返回通知
public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
} //异常通知
public void doAfterThrowing(JoinPoint jp, Throwable ex){
System.out.println("异常通知");
System.out.println("异常信息:" + ex.getMessage());
}
}

4)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: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-2.5.xsd"> <bean id="studentService" class="com.cy.service.impl.StudentServiceImpl"></bean>
<bean id="studentServiceAspect" class="com.cy.advice.StudentServiceAspect"></bean> <aop:config>
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<aop:pointcut expression="execution(* com.cy.service..*.*(..))" id="businessService"/>
<aop:before method="doBefore" pointcut-ref="businessService"/>
<aop:after method="doAfter" pointcut-ref="businessService"/>
<aop:around method="doAround" pointcut-ref="businessService"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect>
</aop:config> </beans>

5)测试代码:

public class T {

    public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService = (StudentService) ac.getBean("studentService");
studentService.addStudent("张三");
} }

1)没有异常抛出时,没有异常通知;console打印:

2)addStudent方法中构造异常,有异常通知,console打印:

StudentServiceImpl.java:

package com.cy.service.impl;

import com.cy.service.StudentService;

public class StudentServiceImpl implements StudentService {

    @Override
public void addStudent(String name) {
System.out.println("添加学生"+name);
System.out.println(1/0);
} }

doAround方法中抛出异常是这句话:

--  Object retVal = pjp.proceed();

峰Spring4学习(6)spring AOP的应用例子的更多相关文章

  1. CgLib动态代理学习【Spring AOP基础之一】

    如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...

  2. JavaEE学习之Spring AOP

    一.基本概念 AOP——Aspect-Oriented Programming,面向切面编程,它是spring框架的一个重要组成部分.一般的业务逻辑都有先后关系,我们可以理解为纵向关系,而AOP关注的 ...

  3. Java动态代理学习【Spring AOP基础之一】

    Spring AOP使用的其中一个底层技术就是Java的动态代理技术.Java的动态代理技术主要围绕两个类进行的 java.lang.reflect.InvocationHandler java.la ...

  4. spring深入学习(四)-----spring aop

    AOP概述 aop其实就是面向切面编程,举个例子,比如项目中有n个方法是对外提供http服务的,那么如果我需要对这些http服务进行响应时间的监控,按照传统的方式就是每个方法中添加相应的逻辑,但是这些 ...

  5. Spring学习十三----------Spring AOP的基本概念

    © 版权声明:本文为博主原创文章,转载请注明出处 什么是AOP -面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 -主要的功能是:日志记录.性能统计.安全控制.事务处理. ...

  6. c# spring aop的简单例子

    刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...

  7. 峰Spring4学习(8)spring对事务的支持

    一.事务简介: 二.编程式事务管理: 例子 1.需求:模拟转账,张三向李四转账50元: 数据库中存在t_count表: 代码实现: BankDao.java: package com.cy.dao; ...

  8. 峰Spring4学习(4)spring自动装配

    一.自动装配: Model类: People.java: package com.cy.entity; public class People { private int id; private St ...

  9. 峰Spring4学习(7)spring对JDBC的支持

    第一节: 工程结构: 1)student.java: package com.cy.model; public class Student { private int id; private Stri ...

随机推荐

  1. [BZOJ1901]Dynamic Rankings

    Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1 ],a[i+2]……a[j]中第k小的数 ...

  2. SpringBoot使用Redis数据库

    (1)pom.xml文件引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> &l ...

  3. python 去除不可见的控制字符

    尤其是在json load的时候,字符串中的不可见控制字符可能会导致错误,应该先对字符串进行控制字符过滤. 对网页文本同样适用,最好在处理网页文本时先进性控制字符清洗. Replace null by ...

  4. HDU 1698 Just a Hook(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意:给出1~n的数,每个数初始为1,每次改变[a,b]的值,最后求1~n的值之和. 思路: 区间更新题目 ...

  5. 前端工程化 - npm

    什么是npm npm的全称Node Package Manager,npm原先只是作为nodejs的包管理工具,然而随着前端社区的发展,如今npm不仅是nodejs的包管理工具,还是前端js的包管理工 ...

  6. 爬虫模拟登陆之formdata表单数据

    首先HTTP协议是个无连接的协议,浏览器和服务器之间是以循环往复的请求回复来交互的,交互的形式是以文件形式来进行的.比如在chrome开发者工具network中看到了 每一行是一个文件,又文件大小啊, ...

  7. Sublime Text 3 配置文件路径修改

    Sublime Text 3安装完以后(安装过程不再演示),第一次打开会在C:\Users\admin\AppData\Roaming目录下创建一个Sublime Text 3目录用于存放Sublim ...

  8. java set初始化问题

    set在执行add方法时,多次报空指针异常,后来发现Set初始化时,如果是 Set<Type> set = null; 这样的话,在执行 set.add(element)的时候会报空指针异 ...

  9. TitanX Server安装Caffe

    服务器是Ubuntu Server 16.04,可以ssh和vnc连接. 安装caffe步骤 1. 安装anaconda2:这里不能用3,不知什么原因,cmake错误,无法生成pycaffe 2. 安 ...

  10. ASP.NET Page 指令

    一些重要的Page指令 虽然Page公开了很多属性,让我们可以在运行时调整它的状态与行为,但是,还有些重要的参数却是以“指令”方式提供的,需要在设计时就指定.下面是我整理的一些我认为 比较重要并且经常 ...