Spring Aop 操作

1.AOP的相关操作
  1.基于Aspectj的XML方式
    a.Aspectj简介   
      1.Aspectj是一个面向切面的框架,它拓展了Java语言,Aspectj定义了AOP的基本语法
        所以有一个专门的编译器用来生成遵守Java字节编码的Class文件;
      2.Aspectj是一个基于java的AOP框架;
      3.Spring2.0之后新增了对Aspectj切点的表达式的支持;
      4.@Aspectj是Aspectj1.5新增的功能,通过JDK5注解技术,允许直接在Bean类中定义切面;
      5.新版的Spring框架建议使用Apectj方式开发Aop;
      6.使用Aspectj需要导入Spring AOP和Aspectj相关jar包;
  2.Spring的AOP的操作
    1.在Spring里面进行AOP的操作,使用aspectj实现;
    2.Aspectj不是spring的部分,和spring一起使用进行AOP的操作;
    3.Spring2.0新增了对Aspectj的支持;
  3.使用aspectj实现AOP的两种方式
    1.基于aspectj的xml方式
    2.基于aspectj的注解方式
  4.AOP开发的准备
    1.除了导入基本的jar外,还需要导入aop相关的 jar 


    2.引入aop的约束
    3.使用表达式配置切入点;
     a.切入点:实际增强的方法;
     b.常用表达式
       excution(访问修饰符 返回类型 方法名 参数)
   5.基于Aspectj的XML方式
   

public class Student {
	public void before1(){
		System.out.println("前置增强");
	}
}
public class Teacher {
	public void say(){
		System.out.println("hello");
	}
}
public class TestStudent {
   @Test
	  public void testStudent(){
		 //1.加载spring配置文件,创建对象
		   ApplicationContext context=new ClassPathXmlApplicationContext("spring/applicationContext-common.xml");
		 //2.获取配置创建的对象
		  Teacher student=(Teacher)context.getBean("teacher");
		  student.say();	   
	 }
}

   6.Aspectj的其他操作

<!-- 1.配置对象 -->
      <bean id="teacher" class="com.study.cn.model.Teacher"></bean>
      <bean id="student" class="com.study.cn.model.Student"></bean>
      <!-- 2.配置Aop操作 -->
      <aop:config>
         <!--2.1配置切入点  -->
         <aop:pointcut expression="execution(* com.study.cn.model.Teacher.*(..))" id="pointcut1"/>
         <!-- 2.2配置切面 把增强用到方法上去 -->
          <aop:aspect ref="student">
             <!--配置增强类型 method:增强类里面使用哪个方法作为前置 -->
              <aop:before method="before1" pointcut-ref="pointcut1"/>
              <aop:after-returning method="after1" pointcut-ref="pointcut1"/>
              <aop:around method="round1" pointcut-ref="pointcut1"/>
          </aop:aspect>
      </aop:config>


 

public class Student {
	public void before1(){
		System.out.println("前置增强");
	}
	public void after1(){
		System.out.println("后置置增强");
	}
	public void round1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		System.out.println("方法之前");
		proceedingJoinPoint.proceed();
		System.out.println("方法之后");
	}

}

 7.基于aspectj的注解方式
      a.导入jar,创建对象;

<!-- 1.配置对象 -->
      <bean id="teacher" class="com.study.cn.model.Teacher"></bean>
      <bean id="student" class="com.study.cn.model.Student"></bean>


      b.在spring核心配置文件中,开启aop操作

 <!--开启 AOP操作 -->
      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


      c.在增强类上面用注解完成aop操作

@Aspect
public class Student {
	@Before(value="execution(* com.study.cn.model.Teacher.*(..))")
	public void before1(){
		System.out.println("前置增强");
	}
	@After(value="execution(* com.study.cn.model.Teacher.*(..))")
	public void after1(){
		System.out.println("后置置增强");
	}
	@Around(value="execution(* com.study.cn.model.Teacher.*(..))")
	public void round1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		System.out.println("方法之前");
		proceedingJoinPoint.proceed();
		System.out.println("方法之后");
	}
}
public class Teacher {
	public void say(){
		System.out.println("hello");
	}
}


public class TestStudent {
   @Test
	  public void testStudent(){
		 //1.加载spring配置文件,创建对象
		   ApplicationContext context=new ClassPathXmlApplicationContext("spring/applicationContext-common.xml");
		 //2.获取配置创建的对象
		  Teacher student=(Teacher)context.getBean("teacher");
		  student.say();	   
	 }
}


    8.@Aspectj提供了不同的通知类型
1.@Before 前置通知,相当于BeforeAdvice;
2.@AfterReturning后置通知,相当于AfterReturningAdvice;
3.@Around 环绕通知,相当于MethodInterceptor;
4.@AfterThrowing 抛出通知,相当于ThrowAdvice
5.@After最终通知,不管是否异常,该通知都会执行;

猜你喜欢

转载自blog.csdn.net/qq_29393273/article/details/87792861