一、面向切面编程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. 恢复 MSSQL bak 文件扩展名数据(下)

    恢复 MSSQL bak 文件扩展名数据 一.概念: RESTORE Statements (Transact-SQL) Restores backups taken using the BACKUP ...

  2. Oracle 中wmsys.wm_concat拼接字符串,结果过长报错解决

    备忘:这个函数最大是4000,根据拼接列的长度,通过限制拼接条数来防止拼接字符串过长错误 --这个情况是从子表中读取出具,这里直接把它当做查询字段处理,在子表中有所有数据 select info.id ...

  3. HTML5 简单Demo1

    ----------------------------页面效果图------------------------------ 截图1: 截图2: -------------------------- ...

  4. svn 冲突Skipped ‘inm/inm/templates‘ -- Node remains in conflict

    svn在删除后,提交,更新操作后可能会报, svn update inm/inm -r 1586 Updating ‘inm/inm‘: Password: Skipped ‘inm/inm/temp ...

  5. 2016级算法第三次上机-A.Bamboo的小吃街

    A Bamboo的小吃街 分析 经典的两条流水线问题,题目描述基本类似于课件中的流水线调度,符合动态规划最优子结构性质 关键的动态规划式子为: dp[0][j] = min(dp[0][j - 1], ...

  6. SpringBoot 启动的时候提示 Field *** in *** required a bean named 'entityManagerFactory' that could not be found.

    错误截图 后面发现原来和入口类代码有关. //@SpringBootApplication(scanBasePackages = {"org.jzc.odata.cboard",& ...

  7. [Alpha]Scrum Meeting#2

    github 本次会议项目由PM召开,时间为4月2日晚上10点30分 时长25分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写内置问卷(issue#3) 撰写团队贡献分配计划(issue#39) ...

  8. Linux下如何将文件下载到指定目录

    一.问题描述 当我在搭服务器的时候,发现由于下载的东西太多,所以需要将一些安装包下载到指定的目录下. 二.解决办法 wget -P /usr/test http://download.redis.io ...

  9. 1.使用frp穿透内网

    1.前因后果 1.1弃用ngrok 为节约服务器成本,花了500多块买了一个华为云得1G 1核心 5M得云服务器.然后用ngrok来穿透内网.一直用得还  但是今天在弄nginx得时候发现 ngrok ...

  10. unity 渲染第一步

    unity 不是将宇宙投影到水晶球里,而是:将整个 view frustum 投影成 一个 cube .------ <unity 渲染箴言> 观察一下,整个 view frustum 以 ...