Spring Mvc那点事---(27)Spring Mvc基于aspect的AOP实现

时间:2022-08-07 06:15:02

   spring中通过使用aspect注解,不需要在配置文件中进行配置,就可以实现切面编程.

   需要引用如下JAR包

  

<dependencies>

<!-- spring 组件配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
   创建业务类

  

package com.springdemo.aopdeom;

import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
public class Ticket {

public void SaleTicket()
{
System.out.println("售票");
}


}
创建切面

package com.springdemo.aopdeom;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(1)
@Aspect
public class TicketAspect {

@Pointcut("execution(* com.springdemo.aopdeom.*.*(..))")
public void TicketMethod()
{

}

@Before(value = "execution(* com.springdemo.aopdeom.*.*(..))")
public void doBefore(JoinPoint jp) {
System.out.println("执行前准备,方法名称"+jp.getSignature().getName()+",时间:"+new Date());

}

@AfterReturning(value = "TicketMethod()")
public void doAfter(JoinPoint jp) {

System.out.println("执行后,时间:"+new Date());
}


@Around(value = "execution(* com.springdemo.aopdeom.*.*(..))")
public void doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("执行前环绕\n");
Object result = pjp.proceed();
System.out.println("执行后环绕\n");
}


@AfterThrowing(value = "execution(* com.springdemo.aopdeom.*.*(..))", throwing = "e")
public void doThrow(JoinPoint jp, Throwable e) {
System.out.println("出现异常");
}


}

配置文件aop.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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">

<context:component-scan base-package="com.springdemo.aopdeom" />
<!-- 打开aop 注解 -->
<aop:aspectj-autoproxy />

</beans>

package com.springdemo.aopdeom;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App {    public static void main( String[] args )    {    	 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/springdemo/aopdeom/aop.xml");    	 Ticket t = (Ticket)context.getBean("ticket");    	 t.SaleTicket();    }}
1.前置通知/环绕通知(proceed方法执行之前)--执行顺序不确定
2.被通知方法
3.后置通知/环绕通知(proceed方法执行之后)--执行顺序不确定