Spring AOP 的实现
软件152 余建强
1 使用 API 实现 AOP
新建一个用户接口:UserService
package com.cqvie.aop.api; public interface UserService { public void add(String name);
public void update(String name);
public void delete(String name);
public void select(String name);
}
实现接口类:UserServiceImpl
package com.cqvie.aop.api; public class UserServiceImpl implements UserService { @Override
public void add(String name) {
System.out.println("Add User " + name + " SUCCESS!");
} @Override
public void update(String name) {
System.out.println("Update User " + name + " SUCCESS!");
} @Override
public void delete(String name) {
System.out.println("Delete User " + name + " SUCCESS!");
} @Override
public void select(String name) {
System.out.println("Select User " + name + " SUCCESS!");
} }
写一个日志类,包括前置通知和后置通知:Log
package com.cqvie.aop.api; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice; public class Log implements MethodBeforeAdvice, AfterReturningAdvice { /**
* 前置通知
* @param method 被调用方法对象
* @param args 被调用的方法参数
* @param target 被调用的方法的目标对象
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + " 的 " +
method.getName() + "方法被执行···");
} /**
* 后置通知
* @param returnValue 返回值
* @param method 被调用的方法对象
* @param args 被调用的方法对象的参数
* @param target 被调用的方法对象的目标对象
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + " 的 " +
method.getName() + "方法已成功执行!返回值为:" + returnValue);
System.out.println();
}
}
配置 Spring 的配置文件:applicationContext01.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"> <bean id="userService" class="com.cqvie.aop.api.UserServiceImpl"></bean>
<bean id="log" class="com.cqvie.aop.api.Log"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.cqvie.aop.api.UserServiceImpl.*(..))" id="pointcut"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
</aop:config> </beans>
添加一个测试类:Test
package com.cqvie.aop.api; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { @SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext01.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.update("AngeYu");
userService.delete("AngeYu"); } }
运行结果:
2 自定义类实现 AOP
接口和接口的实现类不变,依旧是上面所提到的 UserService 和 UserServiceIpml
自己写一个日志类 Log,包含前置通知和后置通知
package com.cqvie.aop.custom; public class Log { public void before() {
System.out.println("----- method start -----");
} public void after() {
System.out.println("----- method end -----");
} }
配置 Spring 的配置文件:applicationContext02.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"> <bean id="userService" class="com.cqvie.aop.custom.UserServiceImpl"></bean>
<bean id="log" class="com.cqvie.aop.custom.Log"></bean>
<aop:config>
<aop:aspect ref="log">
<aop:pointcut expression="execution(* com.cqvie.aop.custom.UserServiceImpl.*(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config> </beans>
添加一个测试类:Test
package com.cqvie.aop.custom; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { @SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext02.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.update("AngeYu"); } }
运行结果:
3 使用注解实现 AOP
接口和接口的实现类不变,依旧是上面所提到的 UserService 和 UserServiceIpml
自己写一个日志类 Log,包含前置通知、后置通知、环绕通知
package com.cqvie.aop.annotation; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; @Aspect
public class Log { @Before("execution(* com.cqvie.aop.annotation.UserServiceImpl.*(..))")
public void before() {
System.out.println("----- method start -----");
} @After("execution(* com.cqvie.aop.annotation.UserServiceImpl.*(..))")
public void after() {
System.out.println("----- method end -----");
} @Around("execution(* com.cqvie.aop.annotation.UserServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("--- around start ---");
System.out.println("方法签名:" + pj.getSignature());
Object result = pj.proceed();
System.out.println("--- around end ---");
return result;
} }
配置 Spring 的配置文件:applicationContext03.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"> <bean id="userService" class="com.cqvie.aop.annotation.UserServiceImpl"></bean>
<bean id="log" class="com.cqvie.aop.annotation.Log"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
添加一个测试类:Test
package com.cqvie.aop.annotation; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { @SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext03.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.update("AngeYu"); } }
运行结果:
Spring AOP 的实现的更多相关文章
- 学习AOP之深入一点Spring Aop
上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...
- 学习AOP之认识一下Spring AOP
心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...
- spring aop
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...
- spring aop注解方式与xml方式配置
注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...
- 基于Spring AOP的JDK动态代理和CGLIB代理
一.AOP的概念 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...
- Spring AOP详解
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- Spring AOP实例——异常处理和记录程序执行时间
实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...
- 从零开始学 Java - Spring AOP 实现用户权限验证
每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...
- 从零开始学 Java - Spring AOP 实现主从读写分离
深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...
- 从零开始学 Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
随机推荐
- IDEA13 SVN配置
这个算是解决了,idea13是支持svn 1.8. 步骤: 1.下载svn客户端软件,小乌龟:TortoiseSVN.安装的时候,一定要选择安装svn命令行的那个选项.当前版本1.8默认只会忽略命令行 ...
- 再也不用线上倒数据了,使用 Faker 来造一批假的数据吧。
背景每当建表之后,常常需要写一批假的数据,用于测试算法.数据量的压力测试.列表翻页. 查看详情.数据关联等.这时就需要借助一款造数据的工具,它就是今天所要介绍的 Faker. 介绍 Faker 这个工 ...
- KVM NAT网络模式配置
NAT方式原理 NAT方式是kvm安装后的默认方式.它支持主机与虚拟机的互访,同时也支持虚拟机访问互联网,但不支持外界访问虚拟机. 检查当前的网络设置: #virsh net-list --all N ...
- DotNetty 使用ByteToMessageDecoder 国家部标808协议封装
DotNetty 开源地址 https://github.com/Azure/DotNetty 个人博客地址 http://www.dncblogs.cn/Blog/ShowBlog/70 1.国 ...
- Openlayers地图量算功能
http://openlayers.org/en/latest/examples/measure.html?q=measure 按官网的例子来就行,新建对象时注意加上命名空间 var vect ...
- 企业项目开发--本地缓存guava cache(1)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.在实际项目开发中,会使用到很多缓存技术,而且数据库的设计一般也会依赖于有缓存的情况下设计. 常用的缓存分 ...
- git提交忽略文件或文件夹
在项目根目录下面 添加 .gitignore文件 文件中每一行表示需要忽略的文件的正则表达式. .gitignore文件过滤有两种模式,开放模式和保守模式 1. 开放模式负责设置过滤哪些文件和文件夹 ...
- Elasticsearch入门 + 基础概念学习
原文地址:https://www.cnblogs.com/shoufeng/p/9887327.html 目录 1 Elasticsearch概述 1.1 Elasticsearch是什么 1.2 E ...
- KNN算法的实现(R语言)
一 . K-近邻算法(KNN)概述 最简单最初级的分类器是将全部的训练数据所对应的类别都记录下来,当测试对象的属性和某个训练对象的属性完全匹配时,便可以对其进行分类.但是怎么可能所有测试对象都会找到 ...
- leetcode-179-Largest Number(理解规则,自定义cmp函数进行排序)
题目描述: 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说明 ...