spring学习笔记四:AOP
AOP(Aspect Orient Programming),面向切面编程,是对面向对象编程OOP的一种补充
面向对象编程使用静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运行过程
AOP底层,就是采用动态代理模式实现的。采用了两种代理:JDK的动态代理域CGLIB的动态代理
AOP编程属于:
1.切面(Aspect)
切面泛指交叉业务逻辑
2.织入(weaving)
织入是指将切面代码插入到目标对象的过程
3.切入点(Pointcut)
切入点指切面具体织入的位置
4.目标对象(Target)
目标对象指将要被增强的对象
5.通知(Advice)
通知是切面的一种具体实现,可以完成简单织入功能
6.顾问(Advisor)
顾问是切面的另一种实现,能够将通知以更为复杂的方式织入到目标对象中,是将通知包装为更复制切面的装配器
Spring的AOP编程环境搭建
1.创建Maven项目,配置spring的AOP需要的jar包
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>SrpingTest</groupId>
- <artifactId>SrpingTest</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <build>
- <sourceDirectory>src</sourceDirectory>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.6.1</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>4.3.8.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.3.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjrt</artifactId>
- <version>1.8.2</version>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.8.2</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>4.3.2.RELEASE</version>
- </dependency>
- </dependencies>
- </project>
2.创建接口UserService.java
- package com.agent.service;
- public interface UserService {
- void addUser(String name, String password);
- }
3.创建实现类UserServiceImpl.java
- package com.agent.service.impl;
- import org.springframework.stereotype.Service;
- import com.agent.service.UserService;
- @Service(value="userService")
- public class UserServiceImpl implements UserService {
- @Override
- public void addUser(String name, String password) {
- System.out.println("UserServiceImpl.addUser()...... name: " + name + "; password: " + password);
- }
- }
4.创建AOP的实现,用来记录日志,在方法执行时,记录下方法名和参数的日志工具,LogUtil.java
- package com.agent.aop;
- import org.aspectj.lang.JoinPoint;
- public class LogUtil {
- public void logWrited(JoinPoint point) {
- StringBuffer sb = new StringBuffer();
- sb.append("记录日志--> 执行方法: " + point.getSignature().getName() + "; 传入参数: [");
- // sb.append("记录日志--> 执行方法: " + point.getSignature().getDeclaringType() + "; 传入参数: [");
- // sb.append("记录日志--> 执行方法: " + point.getSignature().getDeclaringTypeName() + "; 传入参数: [");
- Object[] objectArray = point.getArgs();
- for(int i=0; i<objectArray.length; i++) {
- if(objectArray[i] instanceof String) {
- sb.append(String.valueOf(objectArray[i])).append(", ");
- }
- }
- sb.append("]");
- System.out.println(sb.toString());
- }
- }
5.配置spring的配置文件ApplicationContext.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"
- xmlns:tx="http://www.springframework.org/schema/tx"
- 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
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd">
- <context:component-scan base-package="com.agent" />
- <bean id="aspect" class="com.agent.aop.LogUtil" />
- <aop:config>
- <aop:aspect ref="aspect">
- <aop:pointcut expression="execution(* add*(..))" id="mypointcut"/>
- <aop:after method="logWrited" pointcut-ref="mypointcut"/>
- </aop:aspect>
- </aop:config>
- </beans>
6.创建测试方法AOPTest.java
- package com.agent.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.agent.service.UserService;
- public class AOPTest {
- public static void main(String[] args) {
- ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
- UserService us = (UserService)ac.getBean("userService");
- us.addUser("张三", "188");
- }
- }
7.执行测试方法查看结果:
spring学习笔记四:AOP的更多相关文章
- Spring学习笔记之aop动态代理(3)
Spring学习笔记之aop动态代理(3) 1.0 静态代理模式的缺点: 1.在该系统中有多少的dao就的写多少的proxy,麻烦 2.如果目标接口有方法的改动,则proxy也需要改动. Person ...
- Spring学习笔记4——AOP
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- Spring学习笔记四 整合SSH
三大框架架构(整合原理) 步骤1:导包 Hibernate包 1.Hibernate包,hibernate/lib/required 2.hibernate/lib/jpa | java persis ...
- Spring学习笔记四:SpringAOP的使用
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6776247.html 一:AOP基础概念 (1)通知(增强)Advice 通知,其实就是我们从众多类中提取出 ...
- [Spring学习笔记 4 ] AOP 概念原理以及java动态代理
一.Spring IoC容器补充(1) Spring IoC容器,DI(依赖注入): 注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入Field(注解) ...
- Spring学习笔记2—AOP
1.AOP概念 AOP(Aspect Oriented Programming):面向切面编程,AOP能够将那些与业务无关,却为业务模块所共同调用的应用(例如事务处理.日志管理.权限控制等)封装起来, ...
- Spring学习笔记之AOP配置篇(一)
[TOC] 1. 创建并声明一个切面 首先,创建一个类,添加@Component注解使其添加到IoC容器 然后,添加@Aspect注解,使其成为一个切面 最后,在配置文件里面,使用<aop:as ...
- spring学习笔记四:spring常用注解总结
使用spring的注解,需要在配置文件中配置组件扫描器,用于在指定的包中扫描注解 <context:component-scan base-package="xxx.xxx.xxx.x ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
随机推荐
- Java 知识点(一)
博主对 Java知识点的整理基于 c语言,整理内容为 Java的重点及与 c语言的差异点或编程通要知识点.水平有限,欢迎指正.(参考书籍<Java 核心技术 卷Ⅰ>) Java 的类名:名 ...
- 题解【[FJOI2018]所罗门王的宝藏】
本题解同步于luogu emmm切了近年省选题来写题解啦qwq 该题较其他省选题较水吧(否则我再怎么做的出来 思路是图论做法,做法上楼上大佬已经讲的很清楚了,我来谈谈代码实现上的一些细节 \[\tex ...
- OpenMP笔记(三)
个人博客地址:http://www.bearoom.xyz/2019/02/21/openmp3/ 这一部分主要记录一些指令的使用. 一.parallel的使用 parallel是用于构造并行块的,也 ...
- springboot+mybatis+通用mapper+多数据源(转载)
1.数据库准备 数据库表我们在springboot-mybatis数据之外,新建数据库springboot-mybatis2: springboot-mybatis数据库中有t_class表: spr ...
- 图论模型--dijstra算法和floyd算法
matlab代码实现:https://blog.csdn.net/weixin_40108753/article/details/81237585 python代码实现:
- iOS 直接使用16进制颜色
在做iOS开发时,一般我们会吸色,就是产品给的图我们一般会吸色,但是最近吸色时候,老大说有较大的颜色偏差,所以要求我们直接使用UI给出的额16进制颜色,你也可以搜索<RGB颜色值转换成十六进制颜 ...
- RSAUtils加密解密
import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import javax.cr ...
- vue中过滤器filter
Vue.js 允许我们自定义过滤器,可被用作一些常见的文本格式化.过滤器可以用在两个地方:mustache 插值表达式. v-bind表达式.过滤器应该被添加在 JavaScript 表达式的尾部,由 ...
- C/C++预处理指令#include,#define,#undef,#if,#ifdef,#ifndef,#elif,#endif,#error......
本文主要记录了C/C++预处理指令,常见的预处理指令如下: #空指令,无任何效果 #include包含一个源代码文件 #define定义宏 #undef取消已定义的宏 #if如果给定条件为真,则编译下 ...
- elasticsearch ik中文分词器的使用详解
(基于es5.4)先喵几眼github,按照步骤安装好分词器 link:https://github.com/medcl/elasticsearch-analysis-ik 复习一下常用的操作 .查看 ...