博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring实现AOP的三种方式
阅读量:3891 次
发布时间:2019-05-23

本文共 4629 字,大约阅读时间需要 15 分钟。

AOP

AOP:Aspect Oriented Programming (面向切面编程)通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
  1. AOP 的主要功能: 日志记录,性能统计,安全控制,事务处理,异常处理等等。

利用代理模式动态的实现AOP

  1. 首先定义一个接口
public interface UserService {
void add(); void delete(); void modify();}
  1. 定义接口的实现类
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("添加"); } public void delete() {
System.out.println("删除"); } public void modify() {
System.out.println("修改"); }}
  1. 配置日志文件,在方法执行之前,执行日志
import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法 //objects:要被调用的方法的参数 //o:目标对象 public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName() +"的" +method.getName()+"方法被执行了"); }}
  1. 配置日志文件,在方法执行之后执行日志
import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
//o:返回值 //method:被调用的方法 //objects:被调用的方法对象的参数 //o1:被调用的目标对象 System.out.println("执行了"+o1.getClass().getName()+ "的"+method.getName()+"的方法"+ "返回值" + o); }}
  1. 配置 applicationContext.xml 文件
  1. 导入 aspectj 织入包
org.aspectj
aspectjweaver
1.9.2
  1. 测试类
import com.baidu.service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAopTest {
/* 需要加入织入包依赖
org.aspectj
aspectjweaver
1.9.2
*/ @Test public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) context.getBean("userService"); userService.add(); userService.delete(); userService.modify(); }}

下面看一下效果

在这里插入图片描述

自定义类实现AOP

  1. 首先创建一个实体类
public class User {
public void add(){
System.out.println("添加"); }}
  1. 创建自定义类
public class util {
public void before(){
System.out.println("方法执行之前"); } public void after(){
System.out.println("方法执行之后"); }}
  1. 配置bean.xml文件
  1. 测试
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); User user = (User) context.getBean("user"); user.add(); }}
  1. 运行结果
    在这里插入图片描述

利用注解实现AOP

  1. 首先创建实体类
public class User {
public void add(){
System.out.println("添加一个用户"); }}
  1. 创建日志类
import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class Anno {
@Before("execution(* com.baidu.aop3.User.*(..))") public void before(){
System.out.println("方法执行之前"); } @After("execution(* com.baidu.aop3.User.*(..))") public void after(){
System.out.println("方法执行之后"); }}
  1. 编写配置文件beans.xml
  1. 测试类
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); User user = (User) context.getBean("user"); user.add(); }}
  1. 查看结果
    在这里插入图片描述

简单介绍一下Aspectj

  1. Aspect: Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。

  2. Joint point:表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。

  3. Pointcut:表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。

  4. Advice:Advice 定义了在 pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。

转载地址:http://xhohn.baihongyu.com/

你可能感兴趣的文章
Datatables基本初始化——jQuery表格插件
查看>>
Servlet监听器——实现在线登录人数统计小例子
查看>>
Oracle笔记——简单查询语句 Oracle入门
查看>>
基于Hibernate和Struts2的用户管理系统小案例
查看>>
打开.class文件的方法
查看>>
基于windows平台Git+GitHub+Hexo搭建个人博客(一)
查看>>
基于windows平台Git+GitHub+Hexo搭建个人博客(二)
查看>>
Windows平台下SVN安装配置及使用
查看>>
python简便的编辑工具:jupyter notebook
查看>>
使用pip安装的时候出现 ModuleNotFoundError: No module named ‘pip‘
查看>>
Selenium自动化测试(八)之上传文件
查看>>
Selenium UI自动化(Java篇)
查看>>
使用Fiddler模拟弱网进行测试
查看>>
使用POI读取Excel测试用例
查看>>
记一次数据推送的异常解决端口解决
查看>>
linux、mysql、nginx、tomcat 性能参数优化
查看>>
Nginx使用Linux内存加速静态文件访问
查看>>
杀掉nginx进程后丢失nginx.pid,如何重新启动nginx
查看>>
nginx另类复杂的架构
查看>>
Nginx流量复制/AB测试/协程
查看>>