AOP
AOP(Aspect Orieted Programming)意味:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生泛型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑部分之间的耦合性度降低,提高程序的可重用性,同时提高了开发的效率。
Aop在Spring中的作用
提供声明式事务:允许用户自定义切面
- 横切关注点:跨越应用程序多个模块的方法或功能。既是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等。。。
- 切面(ASPECT):和且关注点被模块化的特殊对象。即,他是一个类。
- 通知(Advice):切面必须要完成的工作,即,它是类中的一个方法。
- 目标(Target):被通知的对象。
- 代理(PRoxy):向目标对象应用通知之后创建对象。
- 切入点(PointCut):切面通知执行的“地点”的定义。
- 连接点(JointPoint):与切入点匹配的执行点。
SpringAOP中,通过Advice定义横切逻辑,Spring中支持5中类型的advice
即Aop在不改变原有代码的情况下,去增加新的功能。
使用Spring实现Aop
【重点】使用AOP织入,需要导入一个依赖包
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
方式一:使用Spring的接口
applicationContext.xml
在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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.my.service.ServiceImpl"/>
<bean id="afterLog" class="com.my.log.AfterLog"/>
<bean id="beforeLog" class="com.my.log.BeforeLog"/>
<!--方式一:使用原生Spring APi接口-->
<!--配置Aop,需要导入aop约束-->
<aop:config>
<!--
切入点:pointcut
要执行的位置:execution
*:任意位置
.*:所有方法
(..):方法下的参数
-->
<aop:pointcut id="pointcut" expression="execution(* com.my.service.ServiceImpl.*(..) )"/>
<!--执行环绕增加
将afterLog类切入到pointcut(最终的方法上)这个点
-->
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
要切入的接口
public interface UserService {
public void insert();
public void delete();
public void update();
public void query();
}
被代理对象
public class ServiceImpl implements UserService {
@Override
public void insert() {
System.out.println("添加一条");
}
@Override
public void delete() {
System.out.println("删除一条");
}
@Override
public void update() {
System.out.println("修改一条");
}
@Override
public void query() {
System.out.println("查询一条");
}
}
要切入的事务(要实现接口 )
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"返回值为:"+returnValue);
}
}
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的目标对象方法
//args参数
//target目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
测试
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理的是接口:注意点
UserService userService = (UserService) context.getBean("userService");
userService.delete();
}
}
执行结果
方式二:自定义类
<bean id="userService" class="com.my.service.ServiceImpl"/>
<bean id="log" class="com.my.diy.DiyLog"/>
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="log">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.my.service.ServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
要切入的事务
public class DiyLog {
public void after(){
System.out.println("========after=====");
}
public void before(){
System.out.println("=========before======");
}
}
测试
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理的是接口:注意点
UserService userService = (UserService) context.getBean("userService");
userService.delete();
}
}
方式三:注解实现
<!--开启注解支持 jdk:false cglib:true-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
<bean id="userService" class="com.my.service.ServiceImpl"/>
<bean id="annotationLog" class="com.my.diy.AnnotationLog"/>
切面
@Aspect//标注这个类是个切面
public class AnnotationLog {
@Before("execution(* com.my.service.ServiceImpl.*(..))")
public void before(){
System.out.println("========before=====");
}
@Around("execution(* com.my.service.ServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
System.out.println(jp.getSignature());//获得签名
jp.proceed();//执行方法
System.out.println("环绕后");
}
}
测试
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理的是接口:注意点
UserService userService = (UserService) context.getBean("userService");
userService.delete();
}
}
结果