AOP有关词汇解释:
1.通知(Advice):在切面的某个特定的连接点上执行的动作,即当程序到达一个执行点后会执行相对应的一段代码,也称为增强处理。通知共有如下5种类型[前置通知 后置通知 返回通知 环绕通知 抛出异常后通知]
2.连接点(JoinPoint):程序执行的某个特定位置,例如类初始化前,类初始化后,方法执行前,方法执行后,方法抛出异常时等,Spring只支持方法级别的连接点,即方法执行前,方法执行后,方法抛出异常时
3.切入点(Pointcut):切入点是一个筛选连接点的过程,因为在你的工程中可能有很多连接点,你只是想让其中几个,在调用这几个方法之前、之后或者抛出异常时干点什么,那么就用切入点来定义这几个方法,让切点来筛选连接点,选中那几个你想要的方法
4.切面(Aspect):切面通常是指一个类,是通知和切入点的结合。到这里会发现连接点就是为了让你好理解切点产生的。通俗来说切面的配置可以理解为:什么时候在什么地方做什么事。切入点说明了在哪里干(指定到方法),通知说明了什么时候干什么
5.引入(Introduction):引入允许我们向现有的类添加新方法或属性
6.织入(Weaving):把切面应用到目标对象来创建新的代理对象的过程,织入一般发生在如下几个时机:
(1)编译时:当一个类文件被编译时进行织入,这需要特殊的编译器才可以做的到,例如AspectJ的织入编译器
(2)类加载时:使用特殊的ClassLoader在目标类被加载到程序之前增强类的字节代码
(3)运行时:切面在运行的某个时刻被织入,SpringAOP就是以这种方式织入切面的,原理应该是使用了JDK的动态代理技术
AOP编程相关依赖:
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- spring aop支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- aspectj支持 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
一个简单的AOP编程项目构建过程:
项目结构:
描述:AOPTest类作为AOP类,B类作为一个服务,App作为一个JUnit测试类。
代码:
服务的类B:
package com.test.javabase.service;
import org.springframework.stereotype.Service;
@Service("b")
public class B {
public void test(){
System.out.println("baseJava");
}
}
JUnit测试类:
package com.test.javabase;
import com.test.javabase.service.B;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("applictionContext.xml");
B b = ctx.getBean("b", B.class);
b.test();
}
}
AOP配置 (Java注解配置 优于 XML配置):
AOP的有关配置是配置在Spring容器配置文件里(默认是applicationContext.xml)
XML配置:
AOP类:
package com.test.aop;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;
/*
*@Component注解通过包扫描可将AOPTest类实例化为bean,
*bean的id/name=类名,实例化的bean被容器管理
*/
@Component
public class AOPTest {
//AOP织如连接点的方法
public void mbefore(JoinPoint joinpoint){
System.out.println("aop前置方法********");
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--包扫描-->
<context:component-scan base-package="com.test"/>
<!--AOP配置-->
<aop:config>
<!--切面配置-->
<aop:aspect ref="AOPTest">
<!--切点配置-->
<!--execution(* com.test.service.*.*(..)) :第一个*表示任意返回值,第二个*表示该目录下的所有的类,第三个*表示类中所有的方法,(..)表示所有参数-->
<aop:pointcut id="aopcutpoint" expression="execution(* com.test.service.A.test())"/>
<!--织入方法配置-->
<aop:before method="mbefore" pointcut-ref="aopcutpoint"></aop:before>
</aop:aspect>
</aop:config>
</beans>
Java注解配置:
AOP类
package com.test.javabase.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect //声明该类为切面
public class AOPTest {
//定义切点
@Pointcut("execution(* com.test.javabase.service.*.*(..))")
public void getCutPoint(){
}
//指明切点的前置方法
@Before("getCutPoint()")
public void doAspect(){
System.out.println("baseJava_doAspect...");
}
}
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"
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">
<!--当使用注解时需要将注解有关的类在容器中注册为bean,使用该语句可自动注册注解有关的bean,当使用包扫描语句时,自动注册注解相关的bean,不需要使用该语句-->
<!--<context:annotation-config/>-->
<!--包扫描-->
<context:component-scan base-package="com.test.javabase"/>
<!--启动aspectJ的自动代理生成功能-->
<aop:aspectj-autoproxy/>
</beans>