AOP_切面声明(XML+注解)

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

@H_502_1@一、XML配置

@H_502_1@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");
	}
}

@H_502_1@
2.定义通知方法

@H_502_1@

@H_502_1@

package cn.xt.aop;

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

@H_502_1@

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>


@H_502_1@

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


@H_502_1@二、注解配置切面

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

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

@H_502_1@

@H_502_1@

<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>


@H_502_1@2.使用@Aspect声明切面

@H_502_1@切面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);
	}
}


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

@H_502_1@

@H_502_1@

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");
	}
}

三、测试

@H_502_1@

@H_502_1@

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");
	}

打印结果

@H_502_1@

@H_502_1@

before,通过织入切面的方法传递参数==abc
around before
test excuted....abc
around after
after
returning

猜你在找的XML相关文章