首先回顾几个名词,能用通俗的话来讲就用通俗的来讲
切面:比如事务,我们要把它单独模块化出来实现,不想影响业务逻辑,那么这个时候我们给业务逻辑添加进入这个事务的功能,它就是切面
连接点:可以让切面具体实现的一些位置集合称为连接点
切点:具体切面切入的位置,为切点
通知:切面要完成的工作,比如事务,保存数据库的正确操作
引入:向现有的类添加属性合方法
织入:把切面应用到目标对象,并创建新的代理对象的过程,在目标对象生命周期里有多个点可以织入:1.编译器 2.类加载器 3.运行期
Spring提供了4种类型的AOP支持
1.基于代理的经典SpringAop
2.纯POJO切面
3.@AspectJ注解驱动的切面
4.注入式AspectJ切面
前3种Spring对AOP的直接局限于方法拦截
PS:Spring只有在需要应用到代理对象才创建它,但是Spring上下文加载后就会完成初始对象的创建
SpringAOP一般使用Spring结合AspectJ,但是SpringAOP并不能使用所有的AspectJ的功能
下面是Spring可以使用AspectJ的一些节点
arg(),@args(),execution(),this(),target(),@target(),within(),@within(),@annotation()
AspectJ有5个重要的通知
1.前置通知
2.后置通知
3.异常通知
4.返回通知
5.环绕通知
继续注解的形式贴上代码:
<运行结果>:
beforeAssit
start living...
afterAssit
returnAssit
I am in before
start having...
I am in returning
I am in after
next one:sam
welcome : sam
package Model;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
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 Assit {
//自定义切点
@Pointcut("execution(* Model.Hotel.live(..))")
public void rest(){}
@Pointcut("execution(* Model.Hotel.having(..))")
public void having(){}
@Pointcut("execution(* Model.Hotel.welcomeImportantPerson(String))"
+ "&&args(name)")
public void welcome(String name){}
//前置通知
@Before("rest()")
public void beforeAssit(){
System.out.println("beforeAssit");
}
//后置通知
@After("rest()")
public void afterAssit(){
System.out.println("afterAssit");
}
@AfterReturning("rest()")
//返回通知
public void returnAssit(){
System.out.println("returnAssit");
}
@AfterThrowing("rest()")
//异常通知
public void exceptionAssit(){
System.out.println("exceptionAssit");
}
//环绕通知
@Around("having()")
public void arroundAssit(ProceedingJoinPoint pp){
System.out.println("I am in before");
try {
pp.proceed();
System.out.println("I am in returning");
} catch (Throwable e) {
System.out.println("I am in exception");
e.printStackTrace();
}
System.out.println("I am in after");
}
@Before("welcome(name)")
public void beforeWelcome(String name){
System.out.println("next one:"+name);
}
}
package Model;
public interface Hotel {
void live();
void having();
void welcomeImportantPerson(String name);
}
package Model;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service("hi")
@Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class HotelImp implements Hotel{
@Override
public void live() {
// TODO Auto-generated method stub
System.out.println("start living...");
}
@Override
public void having() {
// TODO Auto-generated method stub
System.out.println("start having...");
}
@Override
public void welcomeImportantPerson(String name) {
// TODO Auto-generated method stub
System.out.println("welcome : "+name);
}
}