aspectj的aop操作+基于aspectj的注解aop操作

本文介绍了面向切面编程(AOP)的基本概念,包括切入点、通知和切面,并通过实例展示了如何利用Spring框架的手动配置及注解方式实现方法的前置、后置及环绕增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先对于面向切面编程的两个点要清楚:

1.切入点:类中实际被增强的方法称为切入点。

2.通知/增强:增强的逻辑。(比如扩展日志功能)

--通知:前置增强,后置增强,环绕增强

3.切面:把“通知/增强”用到“切入点”当中。

//下面的代码有两个类:Book, MyBook。通过MyBook类的方法增强Book类,Book是切入点。下面首先是不用注解版的代码,手工配置:

package cn.itcast.aop;

public class Book {

	public void add(){
		System.out.println("book.......");
	}
}
package cn.itcast.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {

	public void before1(){
		System.out.println("前置增强.....");
	}
	public void after1(){
		System.out.println("后置增强.....");
	}
	public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		System.out.println("方法之前");
		proceedingJoinPoint.proceed();
		System.out.println("方法之后");
	}
}
package cn.itcast.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {

	public void before1(){
		System.out.println("前置增强.....");
	}
	public void after1(){
		System.out.println("后置增强.....");
	}
	public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		System.out.println("方法之前");
		proceedingJoinPoint.proceed();
		System.out.println("方法之后");
	}
}
<?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.xsd"> <!-- bean definitions here -->

	
	<!-- 配置对象 -->
	<bean id="book" class="cn.itcast.aop.Book"></bean>
	<bean id="mybook" class="cn.itcast.aop.MyBook"></bean>
	
	<!-- 配置aop的操作 -->
	<aop:config>
		<!-- 1.配置切入点(被增强的方法) -->
		<aop:pointcut expression="execution(* cn.itcast.aop.Book.*(..))" id="pointcut1"/>
		<!-- 2.配置切面
			把增强用到具体方法上 -->
		
		<aop:aspect ref="mybook"><!-- 指定增强类 -->
			<!-- 配置增强类型
				method增强类里面那个方法作为前置增强 -->
			
			<aop:before method="before1" pointcut-ref="pointcut1"/><!-- 将增强中的方法用到指定切入点上 -->
			<aop:after method="after1" pointcut-ref="pointcut1"/>
			<aop:around method="around1" pointcut-ref="pointcut1"/>
		</aop:aspect>
	</aop:config> 
</beans> 

 

项目目录截图:

 

 

 

 

 ####################################################################################################

//以下是通过注解完成以上同样内容的代码:

 

package cn.itcast.aop;

public class Book {

	public void add(){
		System.out.println("book.......");
	}
}
package cn.itcast.aop;

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

@Aspect
public class MyBook {

	//在方法上面使用注解完成加强
	@Before(value="execution(* cn.itcast.aop.Book.*(..))")
	public void before1(){
		System.out.println("before1....");
	}
}
package cn.itcast.aop;

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

public class TestAop {

	public static void TestAop(){
		ApplicationContext context = 
				new ClassPathXmlApplicationContext("bean1.xml");
		Book book = (Book)context.getBean("book");
		book.add();
	}
	public static void main(String[] args){
		TestAop.TestAop();
	}
}
<?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.xsd"> <!-- bean definitions here -->

	<!-- 开启aop的扫描 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<!-- 配置对象 -->
	<bean id="book" class="cn.itcast.aop.Book"></bean>
	<bean id="mybook" class="cn.itcast.aop.MyBook"></bean>
	

</beans> 

 项目截图:

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值