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

   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方法执行之后)--执行顺序不确定



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值