spring aop before增强

本文详细介绍如何在Spring框架中使用AOP(面向切面编程),包括配置AOP支持、定义切面类及应用切入点。通过具体代码示例,展示如何在业务逻辑中加入前置通知,实现服务方法调用前的增强。

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

第一步

在spring配置文件添加spring aop支持

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" 
    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.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"
	default-lazy-init="true">
	<!-- 指定自动搜索Bean组件、自动搜索切面类 -->
	<context:component-scan base-package="com.thinkgem.jeesite.modules.test.aop">
		<context:include-filter type="annotation"
			expression="org.aspectj.lang.annotation.Aspect"/>
	</context:component-scan>
	<!-- 启动@AspectJ支持 -->
	<aop:aspectj-autoproxy/>

如图所示
在这里插入图片描述

第二步

定义切面类


package com.thinkgem.jeesite.modules.test.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.test.entity.Test;
import com.thinkgem.jeesite.modules.test.dao.TestDao;

@Aspect
@Service
@Transactional(readOnly = true)
public class TestAopService extends CrudService<TestDao, Test> {

	//com.thinkgem.jeesite.modules.test.service包下所有类,以test开头的方法
	@Before("execution(* com.thinkgem.jeesite.modules.test.service.*.test*(..))")
	public void aopInsert(){
		System.out.println("执行service前执行的aop操作");
	}
}

切面类添加@Aspect,定义切入点@Before("execution( com.thinkgem.jeesite.modules.test.service..test(…))")*
注意execution不要写错,写错会报java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting ‘)’ at character position 11
注意匹配路劲星号后面有一个空格,com.thinkgem.jeesite.modules.test.service..test(…)的第一个星号表示service包下所有类,test*表示类中以test开头的方法
在定义一个controller和一个service

@Controller
@RequestMapping(value = "${adminPath}/test/test")
public class TestController extends BaseController {

	@Autowired
	private TestService testService;
	
	@ModelAttribute
	public Test get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return testService.get(id);
		}else{
			return new Test();
		}
		
	}
	@RequestMapping(value = "testaop")
	public void testAop(HttpServletRequest request){
		testService.testAop();
	}	
@Aspect
@Service
@Transactional(readOnly = true)
public class TestAopService extends CrudService<TestDao, Test> {

	//@Before("execution(* com.thinkgem.jeesite.modules.test.*.*(..))")
	@Before("execution(* com.thinkgem.jeesite.modules.test.service.*.test*(..))")
	public void aopInsert(){
		System.out.println("执行service前执行的aop操作");
	}
}

执行结果

执行service前执行的aop操作
执行service

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值