第一步
在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