项目结构
程序代码
HelloImpl.java
WorldImpl.java
定义切面类
package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
// 定义一个切面
@Aspect
public class AuthAspect
{
// 匹配org.crazyit.app.service.impl包下所有类的、
// 所有方法的执行作为切入点
@Before("execution(* org.crazyit.app.service.impl.*.*(..))")
public void authority()
{
System.out.println("模拟执行权限检查");
}
}
配置
<?xml version="1.0" encoding="GBK"?>
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- 指定自动搜索Bean组件、自动搜索切面类 -->
<context:component-scan base-package="org.crazyit.app.service
,org.crazyit.app.aspect">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy/>
</beans>
测试
链接:
《@Around增强处理简单示例》http://www.cnblogs.com/ssslinppp/p/5845659.html
《@AfterThrowing增强处理简单示例》http://www.cnblogs.com/ssslinppp/p/4633595.html
《@AfterReturning增强处理简单示例》http://www.cnblogs.com/ssslinppp/p/4633496.html
《@After后向增强处理简单示例》http://www.cnblogs.com/ssslinppp/p/4633427.html
《@Before前向增强处理简单示例》 http://www.cnblogs.com/ssslinppp/default.html?page=7
来自为知笔记(Wiz)
附件列表