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 给解决的经历?反正我是有过 ...
随机推荐
- Jenkins中执行docker命令报错
Cannot connect to the Docker daemon. Is the docker daemon running on this host? 在配置Jenkins从Gitlab自 ...
- Django:上传文件或者图片时request.FILES的值为空
在form表单中加上属性 enctype="multipart/form-data"
- C# 获取每一个像素点的RGB
int x, y; x = e.X; y = e.Y; Color pixel = MyImage.GetPixel(x, y); byte R = pixel.R; byte G = pixel.G ...
- C#treeView控件单击事件选中节点滞后问题解决方法
问题描述:在treeView的Click事件中,选中的节点SelectedNode并不是您刚才点击的节点,总是上一次选中的节点,节点选中滞后的问题. 解决方案:在treeView的MouseDown事 ...
- wpf ,只能窗口调整高度,并且设定最小值。
MaxWidth="900" WindowStyle="ToolWindow" ResizeMode="CanResize" WindowS ...
- c# 锁 Interlocked 操作
//定义原子变量 ; //原子级别+1值,如果>=0,说明当前锁为空,可以执行,避免重复执行 ) { if (_serverThread == null || (_serverThread.Is ...
- Hello World! 我的程序员入坑之旅!
先说下本文标题,各行各业都有自己的行规和一些内行人玩的梗什么的,这是我开始写技术博客的第一篇,所以它的标题毫无疑问只能是Hello World! 介绍一下我自己 我算是一个少见的科班出身的开发者了,1 ...
- 如何使用socket进行java网络编程(五)
本篇记录: 1.再谈readLine()方法 2.什么是真正的长连接 最近又参与了一个socket的项目,又遇到了老生常谈的readLine()问题:对方通过其vb程序向我方socketServer程 ...
- docker和定时任务
查看linux信息 cat /etc/issue 以id运行容器docker start 1c3339d7f9a8通过id结束容器 docker kill 1c3339d7f9a8 Ubuntu 安装 ...
- 应该怎么理解 app = Flask(__name__)
初始化生成一个app对象,这个对象就是Flask的当前实例对象,后面的各个方法调用都是这个实例Flask会进行一系列自己的初始化,比如web API路径初始化,web资源加载,日志模块创建等.然后返回 ...