使用spring注解方式实现AOP

使用Spring注解的方式来实现前后置通知、环绕通知等。

分为以下步骤:

1、引入jar文件

2、配置AOP命名空间

3、创建目标对象类

4、创建切面类

 

pom文件

		<dependency>
			<groupId> org.aspectj</groupId>
			<artifactId> aspectjweaver</artifactId>
			<version> 1.8.7</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency>

 

接着创建目标对象类Student.java

package com.kobe.aop.aspect;

public class Student {
	public String print(String name){  
		System.out.println("print() method:" + name);  
		return "hello";  
	}  
}

 

创建切面类 StuInterceptor.java

package com.kobe.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class StuInterceptor {

	/**
	 * AOP打印方法,切点
     * 第一个 * 代表返回值可以是任意类型  第二行的第二个行代表所有方法  ..代表任意个数任意类型的参数
	 */
	@Pointcut("execution(* com.kobe.aop.aspect.Student.print(..))")
	// @Pointcut("execution(* com.kobe.aop.aspect.Student.*(..))")
	public void printMethod() {
	}

	@Before("printMethod()")
	public void printBeforeAdvice() {
		System.out.println("printBeforeAdvice()!");
	}

	@AfterReturning(pointcut = "printMethod()", returning = "flag")
	public void printAfterAdvice(String flag) {
		System.out.println("printAfterAdvice()! " + flag);
	}

	@After("printMethod()")
	public void finallyAdvice() {
		System.out.println("finallyAdvice()!");
	}

	@Around("printMethod() && args(name)")
	public Object printAroundAdvice(ProceedingJoinPoint pjp, String name)
			throws Throwable {
		Object result = null;
		if (name.equals("kobe"))
			pjp.proceed();
		else
			System.out.println("print()方法已经被拦截");
		return result;
	}
}

 

创建配置文件   conf-aspect.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:aop="http://www.springframework.org/schema/aop"
	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-3.1.xsd">
	<aop:aspectj-autoproxy />
	<bean id="stuInterceptor" class="com.kobe.aop.aspect.StuInterceptor"></bean>
	<bean id="stu" class="com.kobe.aop.aspect.Student"></bean>
</beans>    

 

创建测试类  Main.java   

package com.kobe.aop.aspect;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {  
             ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/conf-aspect.xml");  
               Student stu = (Student)ctx.getBean("stu");  
                 stu.print("kobe");
             }  

}

  Run    结果为
195147_NF20_2437179.png

把 stu.print("kobe"); 中的kobe换成其他

结果为:

195414_J0FF_2437179.png

 

 

 

转载于:https://my.oschina.net/xiaozhiwen/blog/1791937

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值