一、面向切面编程AOP

  目标:让我们可以“专心做事”,避免繁杂重复的功能编码

  原理:将复杂的需求分解出不同方面,将公共功能集中解决

 

  *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态添加功能的技术。

二、AOP相关术语

  1.增强处理(Advice)

    前置增强,后置增强,环绕增强、异常抛出增强、最终增强等。。。

  2.切入点(Pointcut)

  3.连接点(Join Point)

  4.切面(Aspect)

  5.目标对象(Target Object)

  6.AOP代理(AOP proxy)

  7.织入(Weaving)

三、使用Spring AOP实现日至输出

  1.在项目中添加Spring AOP的jar文件

  2.编写前置增强和后置增强实现日志功能

package aop;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint; /**
*
* @author TengYiCheng
*
*/
public class UserServiceLogger {
private static final Logger logger = Logger.getLogger(UserServiceLogger.class);
/**
* 代表前置增强的方法
* @param jp
*/
public void before(JoinPoint jp){
logger.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法入参:"+Arrays.toString(jp.getArgs()));
}
/**
* 代表后置增强的方法
* @param jp
* @param result
*/
public void afterReturning(JoinPoint jp,Object result){
logger.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法返回值:"+result);
}
}

  3.编写Spring配置文件,对业务方法进行增强

 <?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.xsd">
<!-- 声明组件 -->
<bean id="dao" class="dao.impl.UserInfoDaoImpl"></bean>
<bean id="service" class="service.impl.UserServiceImpl">
<property name="dao" ref="dao"></property>
</bean>
<bean id="theLogger" class="aop.UserServiceLogger"></bean>
<!-- AOP定义切入点 -->
<aop:config>
<!-- 定义一个切入点表达式 -->
<aop:pointcut id="pointcut" expression="execution(public int addUser(entity.UserInfo))"/>
<!-- 引用包含增强方法的Bean -->
<aop:aspect ref="theLogger">
<!-- 配置前置增强方法 -->
<aop:before method="before" pointcut-ref="pointcut"/>
<!-- 配置后置增强方法 -->
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
</aop:aspect>
</aop:config>
</beans>

  4.编写代码获取带有增强处理的业务对象

package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.UserInfo;
import service.IUserService; /**
*
* @author TengYiCheng
*
*/
public class AOPLoggerTest {
@Test
public void aopTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserService service = (IUserService) context.getBean("service");
UserInfo user = new UserInfo();
user.setId(10010);
user.setUserName("Test");
user.setUserPassword("123456");
user.setPhone("13201069939");
service.addUser(user);
}
}

Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)的更多相关文章

  1. Java 面向切面编程(Aspect Oriented Programming,AOP)

    本文内容 实例 引入 原始方法 装饰者模式 JDK 动态代理和 cglib 代理 直接使用 AOP 框架--AspectWerkz 最近跳槽了,新公司使用了 AOP 相关的技术,于是查点资料,复习一下 ...

  2. Spring学习笔记--面向切面编程(AOP)

    什么是AOP AOP(Aspect Oriented Programming),意为面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的 ...

  3. Spring学习笔记-面向切面(AOP)-04

    什么是面向切面编程 先大概了解一下部分术语 横切关注点:软件开发中,散布于多出的功能称为横切关注点(cross-cutting concern),简单的可以描述为可以影响应用多处的功能,比如日志.安全 ...

  4. spring学习 八 面向切面编程(AOP)概述

    注:本文大部分参考   --------------------- 本文来自 -望远- 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yanquan345/artic ...

  5. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

  6. Spring中的面向切面编程(AOP)简介

    一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...

  7. 程序员笔记|Spring IoC、面向切面编程、事务管理等Spring基本概念详解

    一.Spring IoC 1.1 重要概念 1)控制反转(Inversion of control) 控制反转是一种通过描述(在java中通过xml或者注解)并通过第三方去产生或获取特定对象的方式. ...

  8. Spring框架学习笔记(2)——面向切面编程AOP

    介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...

  9. Spring学习手札(二)面向切面编程AOP

    AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...

随机推荐

  1. Mysql数据操作《一》数据的增删改

    插入数据INSERT 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); 语法二: INSERT INT ...

  2. OCP最新题库052考题解析及答案-第37题

    37.USER1 grants SELECT, INSERT, and UPDATE privileges on USER1. EMP to USER2. SYS executes this comm ...

  3. jqury表单验证

    结合天天生鲜的用户注册页面,学习验证表单js register.js--表单验证源码 $(function(){ var error_name = false; var error_password ...

  4. Ubuntu16.04安装视觉SLAM环境(ceres-solver)

    1.先在github上下载ceres-solver git clone https://github.com/ceres-solver/ceres-solver.git 2.安装ceres-solve ...

  5. Maximum call stack size exceeded

    写vue时报了如下错误 Maximum call stack size exceeded 栈溢出,因为在调用函数时使用了递归调用,而且没有写跳出条件,导致了该错误

  6. 【杂记】linux下各种软件安装方法(持续记录)

    1.安装jdk: 网上一堆说先从windows下压缩包,然后通过共享文件夹copy到linux系统里,然后解压安装,emmmmm 首先进入usr文件夹,新建java文件夹: mkdir java 直接 ...

  7. JavaSwing概述

    GUI(Graphic User Interface)为程序提供图形界面,它最初的设计目的是构建一个通用的GUI,使其能在所有平台上运行.在Java1.0中基础类AWT(Abstract Window ...

  8. 移动APP的用户体验测试

    经常被问到用户体验测试,什么是用户体验测试,用户体验测试要关注的都有哪些呢,现在为大家来罗列一下: 1.横竖屏测试 在移动设备上做用户体验测试,最容易想到的就是对APP做横竖屏的测试来观察APP的显示 ...

  9. android 企业级高性能图表库 SciChart (付费)

    1.官网 https://www.scichart.com/ 2.特性 2.1 链接 https://www.scichart.com/android-chart-features/ https:// ...

  10. pandas中数据框的一些常见用法

    1.创建数据框或读取外部csv文件 创建数据框数据 """ 设计数据 """ import pandas as pd data = {&qu ...