AOP_切面声明(XML+注解)

前端之家收集整理的这篇文章主要介绍了AOP_切面声明(XML+注解)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、XML配置

1.切面


package cn.xt.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class AspectTest {
	public void before(String name){
		System.out.println("before,通过织入切面的方法传递参数=="+name);
	}
	public void after(){
		System.out.println("after");
	}
	public void around(ProceedingJoinPoint jp){
		try {
			System.out.println("around before");
			jp.proceed();
			System.out.println("around after");
		} catch (Throwable e) {
			System.out.println("around throwning");
			e.printStackTrace();
		}
	}
	public void throwing(){
		System.out.println("throwing");
	}
	public void returning(){
		System.out.println("returning");
	}
}


2.定义通知方法


package cn.xt.aop;

public class AdviceMethod {
	public void test(String name){
		System.out.println("test excuted...."+name);
	}
}


3.配置切面


<!-- 声明切面 -->
	<bean class="cn.xt.aop.AspectTest" id="aspectTest"></bean>
	<!-- 声明需要织入切面的方法 -->
	<bean class="cn.xt.aop.AdviceMethod" id="adviceMethod"></bean>

<aop:config>
		<aop:pointcut expression="execution( * cn.xt.aop.AdviceMethod.*(..)) and args(name)" id="pc"/>
		<aop:pointcut expression="execution( * cn.xt.aop.AdviceMethod.*(..))" id="pc2"/>
		<aop:aspect ref="aspectTest">
			<aop:before method="before" pointcut-ref="pc" arg-names="name" />
		</aop:aspect>
		<aop:aspect ref="aspectTest">
			<aop:after method="after" pointcut-ref="pc2" />
			<aop:around method="around" pointcut-ref="pc2" />
			<aop:after-returning method="returning" pointcut-ref="pc2" />
			<aop:after-throwing method="throwing" pointcut-ref="pc2" />
		</aop:aspect>
		
	</aop:config>




对于有参数的通知,如这里的前置通知,通过expression种指定限制条件args以外,还需要用arg-name指定切点的哪个参数引用


二、注解配置切面

使用注解创建切面是AspectJ5以后引入的新特性,大大减少了声明切面的xml配置量

1.注册org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator类,这里不用手动声明这个bean,只需要配置<aop:aspectj-autoproxy/>即可


<aop:aspectj-autoproxy />
	
	<!-- 声明切面 -->
	<bean class="cn.xt.aop.annotation.AspectTest"></bean>
	<bean class="cn.xt.aop.annotation.AspectTest2"></bean>
	<!-- 声明需要织入切面的方法 -->
	<bean class="cn.xt.aop.annotation.AdviceMethod" id="adviceMethod"></bean>


2.使用@Aspect声明切面

切面1(需要切入方法参数的前置通知


package cn.xt.aop.annotation;

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

@Aspect
public class AspectTest {
	@Pointcut("execution( * cn.xt.aop.annotation.AdviceMethod.*(..)) && args(name)")
	public void pc(String name){}
	
	//声明带参数的通知
	@Before("pc(name)")
	public void before(String name){
		System.out.println("before,通过织入切面的方法传递参数=="+name);
	}
}


切面2(不需要切入方法参数的通知


package cn.xt.aop.annotation;

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.Pointcut;

@Aspect
public class AspectTest2 {
	@Pointcut("execution( * cn.xt.aop.annotation.AdviceMethod.*(..))")
	public void pc2(){}
	
	@After("pc2()")
	public void after(){
		System.out.println("after");
	}
	@Around("pc2()")
	public void around(ProceedingJoinPoint jp){
		try {
			System.out.println("around before");
			jp.proceed();
			System.out.println("around after");
		} catch (Throwable e) {
			System.out.println("around throwning");
			e.printStackTrace();
		}
	}
	@AfterThrowing("pc2()")
	public void throwing(){
		System.out.println("throwing");
	}
	@AfterReturning("pc2()")
	public void returning(){
		System.out.println("returning");
	}
}

三、测试


public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/xt/aop/annotation/aop.xml");
		AdviceMethod am = ctx.getBean("adviceMethod",AdviceMethod.class);
		am.test("abc");
	}

打印结果


before,通过织入切面的方法传递参数==abc
around before
test excuted....abc
around after
after
returning
原文链接:https://www.f2er.com/xml/294581.html

猜你在找的XML相关文章