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 给解决的经历?反正我是有过 ...
随机推荐
- QTP之回放模式(ReplayType)
QTP的回放模式有两种,如下所示: 1. Event模式 -- 事件跟踪 2. Mouse模式 -- 鼠标跟踪 Event模式就是我们平时默认用的模式,也就是事件,其实QTP的click方 ...
- python中的 += 与 +
这一部分首先要理解python内存机制,Python中万物皆对象. 对于不可变对象,改变了原来的值,其别名(变量名)绑定到了新值上面,id肯定会改变 对于可变对象,+ 操作改变了值,id肯定会变,而+ ...
- c#中取整方式
主要用到 System 命名空间下的一个数据类 Math ,调用他的方法 一共有三种方式: 第一种 Math.Round:根据四舍五入取整 第二种 Math.Ceiling:向上取整,有小数,整数加1 ...
- Django准备知识-web应用、http协议、web框架、Django简介
一.web应用 Web应用程序是一种可以通过web访问的应用程序(web应用本质是基于socket实现的应用程序),程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件 ...
- node 无脑生成小程序二维码图
RT 新建createwxaqrcode.js: const request = require('request') const fs = require('fs') // eg:生成购物车列表圆形 ...
- python基础知识input到while循环
j周笔记 输入与输出 1.输入 input ('请输入内容')= 字符串 2.输出 print(输出到控制台) 变量vairable 变量就是相当于我们人的名字 1.名字 ...
- 布隆过滤器redis缓存
Bloom Filter布隆过滤器算法背景如果想判断一个元素是不是在一个集合里,一般想到的是将集合中所有元素保存起来,然后通过比较确定.链表.树.散列表(又叫哈希表,Hash table)等等数据结构 ...
- POJ 1243
#include <iostream> #include <algorithm> #include <cmath> #define MAXN 50 using na ...
- Delphi获取IdHTTP1.Get(url)的返回参数
var ss: TStringStream; begin ss := TStringStream.Create(''); idHTTP1.get(url, ss); ss.Positi ...
- Lingo 做线性规划 - Operation Management Applications
Reference: <An Introduction to Management Science Quantitative Approaches to Decision Making, Rev ...