什么是AOP
-
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
-
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。
-
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP在Spring中的作用
-
提供声明式事务;允许用户自定义切面
-
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
-
切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
-
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
-
目标(Target):被通知对象。
-
代理(Proxy):向目标对象应用通知之后创建的对象。
-
切入点(PointCut):切面通知 执行的 “地点”的定义。
-
连接点(JointPoint):与切入点匹配的执行点。
基于Spring实现AOP
- 重点:使用AOP织入,需要导入一个依赖包!
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
通过Spring API 实现
- 编写业务接口:
package com.jiu.service;
public interface UserService {
public void queryAllUser();
}
- 编写业务接口的实现类:
package com.jiu.service.impl;
import com.jiu.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void queryAllUser() {
System.out.println("call " + this.toString() + " queryAllUser");
}
}
- 编写前置增强类:
package com.jiu.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + " call " + method.getName());
}
}
- 编写后置增强类:
package com.jiu.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + " call " + method.getName() + " and return " + returnValue);
}
}
- 编写applicationContext.xml配置文件注册bean,并配置aop切入
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean-->
<bean id="userServiceImpl" class="com.jiu.service.impl.UserServiceImpl"/>
<bean id="beforeLog" class="com.jiu.log.BeforeLog"/>
<bean id="afterLog" class="com.jiu.log.AfterLog"/>
<!-- aop配置-->
<aop:config>
<!-- 切入点:即增强哪个类-->
<!-- execution:表达式匹配要增强的方法-->
<aop:pointcut id="pointcut" expression="execution(* com.jiu.service.impl.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
- 测试:
import com.jiu.service.UserService;
import com.jiu.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userServiceImpl = context.getBean("userServiceImpl", UserService.class);
userServiceImpl.queryAllUser();
}
}
自定义类实现AOP
- 自定义增强类:
package com.jiu.log;
public class DiyLog {
public void before(){
System.out.println("diy aop before");
}
public void after(){
System.out.println("diy aop after");
}
}
- 在applicationContext.xml中注册并配置aop切入
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean-->
<bean id="userServiceImpl" class="com.jiu.service.impl.UserServiceImpl"/>
<bean id="beforeLog" class="com.jiu.log.BeforeLog"/>
<bean id="afterLog" class="com.jiu.log.AfterLog"/>
<bean id="diyLog" class="com.jiu.log.DiyLog"/>
<!-- aop配置-->
<aop:config>
<aop:aspect ref="diyLog">
<aop:pointcut id="pointcut" expression="execution(* com.jiu.service.impl.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
- 测试结果:
使用注解实现
- 编写一个注解实现的增强类:
package com.jiu.log;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnotationPointCut {
@Before("execution(* com.jiu.service.impl.UserServiceImpl.*(..))")
public void before(){
System.out.println("annotation aop point cut before method");
}
@After("execution(* com.jiu.service.impl.UserServiceImpl.*(..))")
public void after(){
System.out.println("annotation aop point cut after method");
}
}
- 在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean-->
<bean id="userServiceImpl" class="com.jiu.service.impl.UserServiceImpl"/>
<bean id="annotationPointCut" class="com.jiu.log.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
</beans>
- 测试结果:
- aop:aspectj-autoproxy:
通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy />隐藏起来了
<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class=“true”/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。