spring aop纯注解

本文深入解析Spring AOP与AspectJ注解的实现原理与使用方法,通过示例代码演示如何在实际项目中应用这些注解进行前置、后置、环绕和异常通知,以及如何通过配置文件和注解扫描实现AOP功能。

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

接口不在贴了,实现类:

package com.leige.aspect3;

import org.springframework.stereotype.Service;


@Service
public class PersonServiceImpl implements  PersonService {

	public void addPerson() {
	System.out.println("person --------addperson");

	}

	
}


定义aspect切面类

package com.leige.aspect3;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component//将aspect交给spring管理
@Aspect//定义切面类
public class MyAspect {
	//前置增强,匹配切入点
	@Before("myPointCut()")
	public void myBefore(JoinPoint joinPoint){
		System.out.println("前置通知, 方法名称:" + joinPoint.getSignature().getName());
	}
	
	public void myAfterReturning(JoinPoint joinPoint,Object xxx){
		System.out.println("后置通知, 返回值:" + xxx);
	}
	
	public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
		
		System.out.println("前");
		//必须执行目标方法,joinPoint可以执行目标方法
		Object obj = joinPoint.proceed();
		
		System.out.println("后");
		return obj;
	}
	
	public void myAfterThrowing(JoinPoint joinPoint, Throwable e){
		System.out.println("抛出异常通知, " + e.getMessage());
	}
	@After("myPointCut()")
	//最终通知
	public void myAfter(){
		System.out.println("最终");
	}
	//定义切入点表达式
	@Pointcut("execution(* com.leige.aspect3..*.*(..))")
	public void myPointCut(){
		
	}
}
xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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"
       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
       					   http://www.springframework.org/schema/context
       					   http://www.springframework.org/schema/context/spring-context.xsd
       					   ">
       					 
<!--      注解开发
首先要配置注解扫描 -->
<context:component-scan base-package="com.leige.aspect3"></context:component-scan>
<!-- 启用aop注解 -->
<aop:aspectj-autoproxy/>
</beans>
测试类:
package com.leige.aspect3;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:com/leige/aspect3/beans.xml")
public class TestApp {
	@Autowired
	PersonService service;
	@Test
	public void fun(){
		/*ApplicationContext context=new ClassPathXmlApplicationContext("com/leige/aspect2/beans.xml");
		PersonService service= (PersonService) context.getBean("personServiceId");*/
		service.addPerson();
		
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值