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.执行测试方法查看结果: