Aspectj的AOP操作

本文介绍如何使用Spring框架实现面向切面编程(AOP),包括基于XML配置和注解的方式,展示了如何定义切面、切入点及各类通知类型。

导包和约束

这里写图片描述

这里写图片描述

基于配置文件的AOP操作

//Book.java

package cn.cstor;

public class Book {

	public void show(){
		System.out.println("come from Book.show()...");
		//int i = 1/0;
	}
}

//EnhancedBook.java

package cn.cstor;

import org.aspectj.lang.ProceedingJoinPoint;

public class EnhancedBook {
	public void before() {
		System.out.println("come from EnhancedBook.before()..");
	}
	
	public void after() {
		System.out.println("come from EnhancedBook.after()..");
	}
	
	public void afterReturnling() {
		System.out.println("come from EnhancedBook.afterReturnling()..");
	}
	
	public void throwing() {
		System.out.println("come from EnhancedBook.throwing()..");
	}
	
	public void around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("come from EnhancedBook.around() start...");
		joinPoint.proceed();
		System.out.println("come from EnhancedBook.around() end...");
	}
}


<!-- applicationContext.xml -->

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	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-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
	
	<!-- 创建Bean对象 -->
	<bean id="book" class="cn.cstor.Book"/>
	<bean id="enhancedBook" class="cn.cstor.EnhancedBook"/>
	
	<!-- AOP配置-->
	<aop:config>
		<!-- 配置切入点 -->
		<aop:pointcut expression="execution(* cn.cstor.Book.*(..))" id="pointcut1"/>
		<!-- 配置切面 -->
		<aop:aspect ref="enhancedBook">
			<aop:before method="before" pointcut-ref="pointcut1"/>
			<aop:after method="after" pointcut-ref="pointcut1"/>
			<aop:after-returning method="afterReturnling" pointcut-ref="pointcut1"/>
			<aop:after-throwing method="throwing" pointcut-ref="pointcut1"/>
			<aop:around method="around" pointcut-ref="pointcut1"/>
		</aop:aspect>
	</aop:config>
	
</beans>
//BookTest.java

package cn.cstor;

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

public class BookTest {
	@Test
	public void f(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		((Book) ac.getBean("book")).show();
	}
}

#基于注解的AOP操作

<!-- applicationContext.xml -->

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	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-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
	
	<bean id="book" class="cn.cstor.Book"/>
	<bean id="enhancedBook" class="cn.cstor.EnhancedBook"/>
	<!-- 开启aop操作 -->
	<aop:aspectj-autoproxy/>
	
</beans>
//EnhancedBook.java

package cn.cstor;

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 EnhancedBook {
	
	@Before("execution(* cn.cstor.Book.*(..))")
	public void before() {
		System.out.println("come from EnhancedBook.before()..");
	}
	@After("execution(* cn.cstor.Book.*(..))")
	public void after() {
		System.out.println("come from EnhancedBook.after()..");
	}
	@AfterReturning("execution(* cn.cstor.Book.*(..))")
	public void afterReturnling() {
		System.out.println("come from EnhancedBook.afterReturnling()..");
	}
	@AfterThrowing("execution(* cn.cstor.Book.*(..))")
	public void throwing() {
		System.out.println("come from EnhancedBook.throwing()..");
	}
	@Around("execution(* cn.cstor.Book.*(..))")
	public void around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("come from EnhancedBook.around() start...");
		joinPoint.proceed();
		System.out.println("come from EnhancedBook.around() end...");
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值