spring通知的注解


1、代理类接口Person.java

 1 package com.xiaostudy;
 2
 3 /**
 4  * @desc 被代理类接口
 5  *
 6  * @author xiaostudy
 7  *
 8  */
 9 public interface Person {
10
11     public void add();
12     public void update();
13     public void delete();
14 }

2、代理类PersonImple.java

 1 package com.xiaostudy;
 2
 3 import org.springframework.stereotype.Component;
 4
 5 /**
 6  * @desc 被代理类
 7  *
 8  * @author xiaostudy
 9  *
10  */
11 @Component("person")//类注解
12 public class PersonImple implements Person {
13
14     /**
15      * @desc 实现接口方法
16      */
17     public void add() {
18         System.out.println("add().....");
19     }
20
21     @Override
22     public void update() {
23         System.out.println("update().....");
24 //        int i = 1/0;
25     }
26
27     @Override
28     public void delete() {
29         System.out.println("delete().....");
30     }
31
32 }

3、通知类MyAspectJ.java

 1 package com.xiaostudy;
 2
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.After;
 6 import org.aspectj.lang.annotation.AfterReturning;
 7 import org.aspectj.lang.annotation.AfterThrowing;
 8 import org.aspectj.lang.annotation.Around;
 9 import org.aspectj.lang.annotation.Aspect;
10 import org.aspectj.lang.annotation.Before;
11 import org.aspectj.lang.annotation.Pointcut;
12 import org.springframework.stereotype.Component;
13
14 /**
15  * @desc 通知类
16  *
17  * @author xiaostudy
18  *
19  */
20 @Component//类注解
21 @Aspect//AspectJ注解
22 public class MyAspectJ {
23
24     //声明公共切入点
25     @Pointcut("execution(* com.xiaostudy.PersonImple.*(..))")
26     public void myPointcut() {
27
28     }
29
30     //前置通知注解,只有一个参数时,value可以省略不写
31     @Before("execution(* com.xiaostudy.PersonImple.*(..))")
32     public void myBefort(JoinPoint joinPoint) {
33         System.out.println("前置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
34     }
35
36     //后置通知注解,当参数大于1时,value必须写
37     @AfterReturning(value="myPointcut()", returning="ret")
38     public void myAfterReturning(JoinPoint joinPoint, Object ret) {
39         System.out.println("后置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
40                 + ", ret: " + ret);
41     }
42
43     //环绕通知注解
44     @Around("myPointcut()")
45     public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
46         System.out.println("环绕通知====前>>>>>>>>>>>");
47         Object obj = joinPoint.proceed();
48         System.out.println("环绕通知====后<<<<<<<<<<<");
49         return obj;
50     }
51
52     //异常通知注解
53     @AfterThrowing(value="myPointcut()", throwing="e")
54     public void myThrowint(JoinPoint joinPoint, Throwable e) {
55         System.out.println("异常通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
56                 + ", e: " + e.getMessage());
57         System.exit(0);
58     }
59
60     //最终通知注解
61     @After("myPointcut()")
62     public void myAfter(JoinPoint joinPoint) {
63         System.out.println("最终通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
64     }
65 }

4、spring配置文件applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7                               http://www.springframework.org/schema/beans/spring-beans.xsd
 8                               http://www.springframework.org/schema/aop
 9                               http://www.springframework.org/schema/aop/spring-aop.xsd
10                               http://www.springframework.org/schema/context
11                               http://www.springframework.org/schema/context/spring-context.xsd">
12     <!-- 扫描注解类 -->
13     <context:component-scan base-package="com.xiaostudy"></context:component-scan>
14     <!-- 确定 AOP注解生效 -->
15     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
16 </beans>

5、测试类Test.java

 1 package com.xiaostudy;
 2
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5
 6 /**
 7  * @desc 测试类
 8  *
 9  * @author xiaostudy
10  *
11  */
12 public class Test {
13
14     public static void main(String[] args) {
15         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
16         Person person = ac.getBean("person", Person.class);
17         person.add();
18         person.update();
19         person.delete();
20     }
21
22 }


spring通知的注解

原文地址:https://www.cnblogs.com/xiaostudy/p/9535947.html

时间: 2024-07-30 20:01:50

spring通知的注解的相关文章

spring AOP + 自定义注解实现权限控制小例子

今天看了一下黑马程序员的视频,上面讲到一个使用spring AOP + 自定义注解的方式来实现权限控制的一个小例子,个人觉得还是可以借鉴,整理出来与大家分享. 需求:service层有一些方法,这些方法需要不同的权限才能访问. 实现方案:自定义一个PrivilegeInfo的注解,使用这个注解为service层中的方法进行权限配置,在aop中根据PrivilegeInfo注解的值,判断用户是否拥有访问目标方法的权限,有则访问目标方法,没有则给出提示. 关键技术:自定义注解及注解解析,spring

Spring AOP基于注解的“零配置”方式

Spring AOP基于注解的“零配置”方式: Spring的beans.xml中 <!-- 指定自动搜索Bean组件.自动搜索切面类 --> <context:component-scan base-package="org.crazyit.app.service,org.crazyit.app.aspect"> <context:include-filter type="annotation" expression="or

spring AOP自定义注解方式实现日志管理

转:spring AOP自定义注解方式实现日志管理 今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven />     <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->     <conte

Spring AOP 使用注解定义切面(转载)

原文地址:http://www.jianshu.com/p/6f40dddd71a5 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audience: package com.spring.aop.service.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aft

(转)利用Spring AOP自定义注解解决日志和签名校验

一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: boolean isValid = accountService.validSignature(appid, signature, client_signature); if (!isValid) return ErrorUtil.buildError(ErrorUtil.ERR_CODE_COM

利用Spring AOP自定义注解解决日志和签名校验

转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: boolean isValid = accountService.validSignature(appid, signature, client_signature); if (!

spring AOP自定义注解 实现日志管理

今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven />     <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->     <context:component-scan base-pac

Spring基于纯注解方式的使用

经过上篇xml与注解混合方式,对注解有了简单额了解,上篇的配置方式极大地简化了xml中配置,但仍有部分配置在xml中进行,接下来我们就通过注解的方式将xml中的配置用注解的方式实现,并最终去掉xml配置. 一.xml中遗留配置 注解扫描 <!-- 开启注解并扫描指定包中带有注解的类 --> <context:component-scan base-package="com.kkb.spring.service"/> 非自定义bean,如sqlsessionFac

spring mvc 方法注解拦截器

应用场景,在方法级别对本次调用进行鉴权,如api接口中有个用户唯一标示accessToken,对于有accessToken的每次请求可以在方法加一个拦截器,获得本次请求的用户,存放到request或者session域. python中,之前在python flask中可以使用装饰器来对方法进行预处理,进行权限处理 先看一个实例,使用@access_required拦截: @api.route('/post_apply') @access_required def apply():     "&q