一.什么是Aop
►AOP(Aspect-OrientedProgramming, 面向切面编程): 是一种新的方法论, 是对传统OOP(Object-OrientedProgramming, 面向对象编程) 的补充.
►AOP的主要编程对象是切面(aspect),而切面模块化横切关注点.
►在应用AOP编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.
►AOP的好处:
§每个事务逻辑位于一个位置, 代码不分散, 便于维护和升级
§业务模块更简洁, 只包含核心业务代码. 非侵入式编程
二.Aop的术语
►切面(Aspect): 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
►通知(Advice): 切面必须要完成的工作
►目标(Target):被通知的对象
►代理(Proxy):向目标对象应用通知之后创建的对象
►连接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点;相对点表示的方位。
►切点(pointcut):每个类都拥有多个连接点:例如 ArithmethicCalculator的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务。AOP 通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过org.springframework.aop.Pointcut接口进行描述,它使用类和方法作为连接点的查询条件。
三.简单的应用
1.创建maven项目,在pom.xml中加入aop的架包
<!-- 面向切面编程 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
2.配置spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"
>
<!-- 扫描器 -->
<context:component-scan base-package="less03.aop"></context:component-scan>
<aop:config>
<!-- 定义切点(搜索条件)
表达式 execution(返回值 包.类.方法(参数))
-->
<aop:pointcut expression="execution(* less03.*.AfHouseOwer.seekMyHouse(..))" id="myPoinCut"/>
<!-- 定义通知 -->
<aop:aspect ref="mymessage">
<!-- method方法 pointcut-ref引用切点
before 前置
after 后置
-->
<aop:before method="beforeseek" pointcut-ref="myPoinCut"/>
<aop:after method="afterseek" pointcut-ref="myPoinCut"/>
<aop:after method="throwException" pointcut-ref="myPoinCut"/>
</aop:aspect>
</aop:config>
</beans>
3.测试代码,切点
package less03.aop;
import org.springframework.stereotype.Component;
@Component
public class AfHouseOwer {
public void seekMyHouse() {
System.out.println("大富出租房屋");
}
}
4.拦截方法
package less03.aop;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;
@Component
public class Mymessage {
//前置通知
public void beforeseek(){
System.out.println("打扫卫生");
}
//后置通知
public void afterseek(){
System.out.println("收钱");
}
//异常通知
public void throwException(JoinPoint jp){
System.out.println("异常");
System.out.println(jp.getKind());
//抛错的方法
System.out.println(jp.getSignature().getName());
}
}
5.运行
package less03.aop;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
/*
* sprign 实现aop 用了两种技术
* 1java动态代理(面向接口编程,一定需要编程)
* 2 cglib实现动态代理
*/
public class TestCotainer {
static ConfigurableApplicationContext context;
//创建容器
static{
context= new GenericXmlApplicationContext("classpath:/less03/aop/spring.xml");
}
public static void main(String[] args) {
//获取容器实例
AfHouseOwer ho=(AfHouseOwer)context.getBean("afHouseOwer");
ho.seekMyHouse();
context.close();
}
}