遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Spring之AOP编程

注意添加:
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
两个jar包


方法调用拦截实现

复制代码
package com.studio.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

@Component
public class MyAdvice implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        
        try{
            
            System.out.println("interceptor-Befor:");
            Object object= invocation.proceed();
            System.out.println("interceptor-End:");
            return object;
        }catch(Exception ex){
            
            System.out.println("interceptor-ex:" + ex.getMessage());
            throw ex;
        }
        
    }

}
View Code
复制代码

 

beens.xml配置文件:

复制代码
<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
            
     <context:component-scan base-package="com.studio.*"/>
       
       
         
     <aop:config>
        <aop:pointcut id="mypointcut" expression="execution(* com..service..*(..))"/>
        <aop:advisor
            advice-ref="myAdvice"
            pointcut-ref="mypointcut"
            order="1" />
        <aop:advisor
            advice-ref="myAdvice2"
            pointcut="execution(* com..service..*(..))"
            order="2" />
    </aop:config>  
     
</beans>
View Code
复制代码

说明
pointcut的execution中“*”表示一个单词(字母组成中间没空格)而“..”表示0或任意多个

定义注解:

复制代码
package com.studio;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckLogin {

}
View Code
复制代码

注解切入点表达式

复制代码
     <aop:config>
        <aop:pointcut id="mypointcut" expression="execution(* com..service..*(..))"/>
        <aop:advisor
            advice-ref="myAdvice"
            pointcut-ref="mypointcut"
            order="1" />
        <aop:advisor
            advice-ref="myAdvice2"
            pointcut="@annotation(com.studio.CheckLogin) 
                      or @target(com.studio.CheckLogin)"
            order="2" />
    </aop:config>  
View Code
复制代码

 说明:
 允许在列或方法上放置CheckUser 以实现拦截
代码在参考华为网盘"软件测试与任务/spring"

参考:
http://blog.csdn.net/topwqp/article/details/8696897
http://blog.sina.com.cn/s/blog_5198c7370100hw1p.html
http://liusg123.iteye.com/blog/941546
http://lavasoft.blog.51cto.com/62575/172292/ 切入点表达式

posted on   遗忘海岸  阅读(395)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示