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 ...
随机推荐
- tomcat添加ssl证书
Tomcat支持JKS格式证书,从Tomcat7开始也支持PFX格式证书,两种证书格式任选其一. 文件说明: 1. 证书文件xxx.pem,包含两段内容,请不要删除任何一段内容. 2. 如果是证书系统 ...
- 2019牛客网暑假多校训练第四场 K —number
链接:https://ac.nowcoder.com/acm/contest/884/K来源:牛客网 题目描述 300iq loves numbers who are multiple of 300. ...
- thinkcmf面包屑制作
网站常有的功能面包屑,如图所示: 支持1级分类 list.html 首页 / {$name} article.html 首页 / {$term['name']} / {$post_title} 链接: ...
- I420转RGB
http://blog.csdn.net/huiguixian/article/details/17288909 public class YuvToRGB { private static int ...
- Fib数列问题(项数很大)
用fib(n)表示斐波那契数列的第n项,现在要求你求fib(n) mod m.fib(1)= 1, fib(2)= 1. 输入格式 输入2个整数n(1≤n≤1018), m(2≤m≤10000000) ...
- 17.3.10--->关于数值溢出问题
取值范围: short.int.long 占用的字节数不同,所能表示的数值范围也不同.以32位平台为例,下面是它们的取值范围: 数据类型 所占字 ...
- build模式入门,build模式理解(转载)
问:为何要用? 普通做法: 1.创建pojo public class Person { private String name; private int age; private double he ...
- Java之同步代码块处理实现Runnable的线程安全问题
/** * 例子:创建三个窗口卖票,总票数为100张.使用实现Runnable接口的方式 * * 1.问题:卖票过程中,出现了重票.错票 -->出现了线程的安全问题 * 2.问题出现的原因:当某 ...
- ZJNU 2133 - 认亲大会
将辈分差距转为数字 例如 A 是 B son A=B-1 A 是 B grandfather A=B+2然后编号1数字设置为0,建图bfs 最后搜索编号2到100是否存在>0的数即可 /* Wr ...
- ZJNU 1223 - 素数距离——高级
因为最大可以达到int极限 明显直接筛选不可能完成 所以从其因子入手 因为任何不是素数的数都有除了1与其自身之外的因子 因此,我们筛出2^(31/2)≍46350之内的所有素数,以其作为因子再将题目给 ...