*用注解非接口增强

导入相关的jar包;

Spring _AspectJAop 使用注解进行增强

2. 引入Spring的配置文件。aop约束;

xmlns:aop="http://www.springframework.org/schema/aop"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:P="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"

       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
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd"

>

</beans>

3.开启AspectJ的注解;在配置文件里;

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 4.编写目标类:

package aspectjaopdemo;

//AspectJAop 使用注解进行增强
public class User {
    public void  speaking(){
        System.out.println("speaking:");

    }
}

5. 编写切面类【增强类】:(最好在同一个包下)

 

package aspectjaopdemo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class UserService {

    @Before("execution(* aspectjaopdemo.User.* (..))")
    public void before1(){
        System.out.println("before.前置增强");
    }
    @After("execution(* aspectjaopdemo.User.* (..))")
    public void After(){
        System.out.println("after: 后置增强");
    }
    @Around("execution(* aspectjaopdemo.User.* (..))")
    public void around(ProceedingJoinPoint pjp){
        System.out.println("该方法执行");
        try {
            pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("该方法结束");
    }

    @AfterThrowing("execution(* aspectjaopdemo.User.* (..))")
    public void afterThrowing(){
        System.out.println("异常抛出增强");
    }
}

 6.在spring 的配置文件里:User类和UserService类的两个对象

   <bean id="user" class="aspectjaopdemo.User"/>
   <bean  id="userService"  class="aspectjaopdemo.UserService"/>

[email protected]测试类:

package test;

import aspectjaopdemo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class aspectjaopdemoTest {
    @Test
    public  void test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("appdatabase.xml");
        User user = (User) context.getBean("user");
        user.speaking();
    }
}

8. 结果:

 Spring _AspectJAop 使用注解进行增强

 总结:

   @Aspect:定义切面类的注解
@Before:前置通知,相当于BeforeAdvice
@AfterReturning:后置通知,相当于AfterReturningAdvice
@Around:环绕通知,相当于MethodInterceptor
@AfterThrowing:抛出通知,相当于ThrowAdvice
@After:最终final通知,不管是否异常,该通知都会执行
@Pointcut:定义切入点的注解
 

 

相关文章:

  • 2022-12-23
  • 2021-12-27
  • 2022-03-10
  • 2022-12-23
  • 2021-12-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
相关资源
相似解决方案