Spring中什么是aop aop最常用的术语是什么
aop 面向切面编程:
就是在一个完整的流程之上 ,我突然想增加一个新的功能,此时我用一个新的模块将这个功能完成,然后通过配置文件或者其他方式将这个模块加入这个流程中去,没有这个模块时这个流程也同样可以正常运行。也就是说在不修改原来代码的情况下对原来的功能进行扩展,这就是aop
#### aop术语:
1.连接点:类里的那些方法可以被增强那么那个方法就可以叫做连接点。
2.切入点:实际被真正增强的方法就叫切入点。
3.通知(增强):实际增强的逻辑部分就叫通知。
通知有多种类型: 前置通知
后置通知
环绕通知
异常通知
最终通知
4.切面:是一个动作,把通知应用到切入点的过程,就叫切面
实际例子
<?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 class="com.qq.pojo.Person"></bean>
<bean id="doopAdvice" class="com.qq.pojo.DoorAdvice"></bean>
<!--注解必须写这句话-->
<aop:aspectj-autoproxy/>
<aop:config>
<aop:pointcut id="enter" expression="execution(public void com.qq.pojo.Person.enter())"/>
<aop:aspect ref="doopAdvice">
<aop:before method="before" pointcut-ref="enter"></aop:before>
出现异常也调用
<aop:after method="after" pointcut-ref="enter"></aop:after>
出现异常不调用
<aop:after-returning method="after_terurning" pointcut-ref="enter"></aop:after-returning>
发生异常执行
<aop:after-throwing method="after_throwing" pointcut-ref="enter"></aop:after-throwing>
<aop:around method="around" pointcut-ref="enter"></aop:around>
</aop:aspect>
</aop:config>-->
</beans>
java通知类
package com.qq.pojo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class DoorAdvice {
@Pointcut("execution(public void com.qq.pojo.Person.enter())")
void point(){
//代表切点
}
@Before("DoorAdvice.point()")
void before(){
System.out.println("before()");
}
@After("DoorAdvice.point()")
void after(){
System.out.println("after()");
}
@AfterReturning("DoorAdvice.point()")
void after_terurning(){
System.out.println("after_terurning()");
}
@AfterThrowing("DoorAdvice.point()")
void after_throwing(){
System.out.println("发生了异常");
}
@Around("DoorAdvice.point()")
void around(ProceedingJoinPoint point) throws Throwable{
System.out.println("3D环绕");
point.proceed();
}
}
测试类
@Test
public void test2(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
Person bean = context.getBean(Person.class);
bean.enter();
}

浙公网安备 33010602011771号