Java框架学习_Spring(五)AOP注解的配置与简单测试、注解的通知类型总结、切入点的注解

前一篇已经对AOP_xml的配置作了学习,这次采用注解的方式,其实大同小异,改一下xml配置和切面类就好了


1、AOP注解的配置与测试:

  1. 导包:Spring_AOP开发jar包
  2. 编写xml配置(默认名applicationContext.xml)
<?xml version="1.0" encoding="utf-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">



<!-- 在配置文件中开启注解的AOP的开发============ -->
<aop:aspectj-autoproxy/>
<!-- 配置目标类================ -->
<bean class="cn.nupt.aopDemo.AopDemo" id="AopDemo"> </bean>
<!-- 配置切面类================ -->
<bean class="cn.nupt.aopClass.AopInsert" id="AopInsert"> </bean>
</beans>
  1. 编写目标类:这里的目标类和前篇没什么区别
package cn.nupt.aopDemo;

public class AopDemo {
	
	public void haha() {
		System.out.println("haha");
	}
	
	public void hehe() {
		System.out.println("hehe");
	}
	
	public void xixi() {
		System.out.println("xixi");
	}

}

  1. 编写切面类(注意,这里加了两个注释@Aspect和@Before,分别代表这是一个切面类和前置增强)
package cn.nupt.aopClass;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//编写切面类

@Aspect
public class AopInsert {
	
	@Before("execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
	public void check() {
		System.out.println("这是采用注解方式权限校验中。。。");
	}

}

  1. 编写测试类:这里的测试类和前篇没什么区别
package cn.nupt.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.nupt.aopDemo.AopDemo;
//junit和aop整合
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
	@Resource(name="AopDemo")
	private AopDemo aopDemo;
	
	@Test
	public void test() {
		
		aopDemo.haha();
		aopDemo.hehe();
		aopDemo.xixi();
	}
	
}
输出:
	haha
	hehe
	这是采用注解方式权限校验中。。。
	xixi

2、注解的通知类型总结:(遇到哪一种按照下面的格式写就行了,比xml配置方便多了)

package cn.nupt.aopClass;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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;

//编写切面类

@Aspect
public class AopInsert {
	
	@Before("execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
	public void check1() {
		System.out.println("前置增强====================");
	}
	
	@AfterReturning(value="execution(* cn.nupt.aopDemo.AopDemo.xixi(..))",returning="result")
	public void check2(Object result) {
		//后置增强可以得到目标函数的返回值
		System.out.println("后置增强====================" + result);
	}
	
	@Around("execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
	public Object check3(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("环绕前增强====================");
		Object object = joinPoint.proceed();
		System.out.println("环绕后增强====================");
		return object;
	}
	

	@AfterThrowing(value="execution(* cn.nupt.aopDemo.AopDemo.xixi(..))",throwing="e")
	public void check4(Throwable e) {
		System.out.println("异常抛出增强====================" + e.getMessage());
	}
	
	@After(value="execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
	public void check5(Throwable e) {
		System.out.println("最终增强====================" + e.getMessage());
	}

}

3 、切入点的注解:
上面切入点的代码太长了,写个注解来给它取个别名

//添加注解取别名为pointcut1
@Pointcut(value="execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
	private void pointcut1(){}

//这个切入点就可以表示为AopInsert.pointcut1()了		
@Before(value="AopInsert.pointcut1()")
	public void check1() {
		System.out.println("前置增强====================");
	}
	

猜你喜欢

转载自blog.csdn.net/weixin_39782583/article/details/86252737