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. 18 11 12 网络通信 tcp

    tec 客户端的接受代码 import socket def main(): # 1. 创建tcp的套接字 tcp_socket = socket.socket(socket.AF_INET, soc ...

  2. java中abstract怎么使用

    abstract(抽象)修饰符,可以修饰类和方法 1,abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型,抽象类就像当于一类的半 ...

  3. Maven--排除依赖

    传递性依赖会给项目隐式地引入很多依赖,这极大地简化了项目依赖的管理,但是有些时候这种特性也会带来问题. 例如,当前项目有一个第三方依赖,而这个第三方的依赖由于某些原因依赖了另外一个类库的 SNAPSH ...

  4. 剑指offer【13】- 链表中倒数第k个结点

    输入一个链表,输出该链表中倒数第k个结点. /* public class ListNode { int val; ListNode next = null; ListNode(int val) { ...

  5. UG NX7.5 采用VS2008调试方法

    1.安装NX7.5(x64),这是废话 2.安装visual studio 2008,推荐安装2008,如果是2010应该也可以用,(没有测试,不清楚) 3.复制 UGS\NX 7.5\UGOPEN\ ...

  6. OpenCV On Android环境配置最新&最全指南(Eclipse篇)

    简介 本教程是经过本人多次踩坑,并参考网上众多OpenCV On Android的配置教程总结而来,尽希望能帮助学习移动图像处理的朋友们少走弯路.这也是本人第一次在简书上发布文章,如有不足,希望各位d ...

  7. 二分查找(python)

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/4/29 9:11 # @Author : Jackendoff # @Sit ...

  8. SQLite-外键约束/表链接查询

    外键约束: 表一的某个字段关联到表二的某个字段 例子: 国家表:t_country

  9. 使用XAMPP集成开发环境安装Apache、PHP的配置说明

    一.安装XAMPP 双击安装包xamppinstaller.exe,可完成XAMPP的安装,与其他软件安装并无区别. 二.配置Apache端口,使用其可以正确启动 点击Start,启动Apache时可 ...

  10. [Algo] 66. All Valid Permutations Of Parentheses I

    Given N pairs of parentheses “()”, return a list with all the valid permutations. Assumptions N > ...