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的更多相关文章

  1. Spring学习笔记之aop动态代理(3)

    Spring学习笔记之aop动态代理(3) 1.0 静态代理模式的缺点: 1.在该系统中有多少的dao就的写多少的proxy,麻烦 2.如果目标接口有方法的改动,则proxy也需要改动. Person ...

  2. Spring学习笔记4——AOP

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  3. Spring学习笔记四 整合SSH

    三大框架架构(整合原理) 步骤1:导包 Hibernate包 1.Hibernate包,hibernate/lib/required 2.hibernate/lib/jpa | java persis ...

  4. Spring学习笔记四:SpringAOP的使用

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6776247.html  一:AOP基础概念 (1)通知(增强)Advice 通知,其实就是我们从众多类中提取出 ...

  5. [Spring学习笔记 4 ] AOP 概念原理以及java动态代理

    一.Spring IoC容器补充(1) Spring IoC容器,DI(依赖注入): 注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入Field(注解) ...

  6. Spring学习笔记2—AOP

    1.AOP概念 AOP(Aspect Oriented Programming):面向切面编程,AOP能够将那些与业务无关,却为业务模块所共同调用的应用(例如事务处理.日志管理.权限控制等)封装起来, ...

  7. Spring学习笔记之AOP配置篇(一)

    [TOC] 1. 创建并声明一个切面 首先,创建一个类,添加@Component注解使其添加到IoC容器 然后,添加@Aspect注解,使其成为一个切面 最后,在配置文件里面,使用<aop:as ...

  8. spring学习笔记四:spring常用注解总结

    使用spring的注解,需要在配置文件中配置组件扫描器,用于在指定的包中扫描注解 <context:component-scan base-package="xxx.xxx.xxx.x ...

  9. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

随机推荐

  1. Python笔记_第四篇_高阶编程_进程、线程、协程_5.GPU加速

    Numba:高性能计算的高生产率 在这篇文章中,笔者将向你介绍一个来自Anaconda的Python编译器Numba,它可以在CUDA-capable GPU或多核cpu上编译Python代码.Pyt ...

  2. php速成_day1

    一.概述 1.什么是PHP PHP ( Hypertext Preprocessor ),是英文超级文本预处理语言的缩写.PHP 是一种 跨平台.嵌入式的服务器端执行的描述语言,是一种在服务器端执行的 ...

  3. 基于SSM开发在线考试系统 Java源码

    实现的关于在线考试的功能有:用户前台:用户注册登录.查看考试信息.进行考试.查看考试成绩.查看历史考试记录.回顾已考试卷.修改密码.修改个人信息等,后台管理功能(脚手架功能不在这里列出),科目专业管理 ...

  4. html分页自适应居中;css设置分页自适应居中

    制作网页列表的分页必不可少,显示的列表条数也不一样,让我们一起来看看如何让分页标签根据给定的分页自动居中呢. 对<ul>标签设置样式为:{ display: table margin:40 ...

  5. 设计模式讲解2:static proxy和decorator的不同点

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 在常见的23种设计模式中,static proxy和decorator在代码结构上是特别相似的.那它们的不 ...

  6. Ubuntu python多个版本管理

    1.查看python有哪些版本使用命令 whereis python 如图: 2.这么多版本如何切换呢 使用 sudo update-alternatives --install <link&g ...

  7. latex学习笔记----数学公式

    https://www.jianshu.com/p/d7c4cf8dc62d 1.数学公式在  \(  和  \)之间,$和$之间,或者\begin{math}和\end{math}之间 2.对于较大 ...

  8. AtCoder Beginner Contest 129

    ABCD 签到(A.B.C过水已隐藏) #include<bits/stdc++.h> using namespace std; ; int n,m,ans,f1[N][N],f2[N][ ...

  9. Python 生成requirements文件以及使用requirements.txt部署项目

    生成requirements.txt 当你的项目不再你的本地时,为了方便在新环境中配置好环境变量,你的项目需要一个记录其所有依赖包以及它们版本号的文件夹requirements.txt 文件. pip ...

  10. 使用Spring Boot和OAuth构建安全的SPA

    最近一段时间都在闭关学习,过程还是有点艰辛的,幸运的是还有优锐课老师带着,少走了很多弯路.很久也没有更新文章了,这篇想和大家分享的是,了解如何在使用Spring Boot入门程序的同时使用Spring ...