spring_AOP_annotation
beans.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: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-2.5.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.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<aop:aspectj-autoproxy />
</beans>
其中<context:annotation-config />为声明使用annotation部分,<context:component-scan base-package="com.bjsxt"/>会使得程序运行时进行对com.bjsxt包及子包的扫描操作,<aop:aspectj-autoproxy />对于AOP的annotation来说最为主要,标志着可以使用AOP的annotion来进行操作。
Aspect
package com.bjsxt.aop; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){}; @Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
} @Before("myMethod()")
public void before() {
System.out.println("method before");
}
}
@Aspect注释标志着此类为一个切面类。
@Pointcut作为切入点,@Pointcut("execution(public * com.bjsxt.service..*.add(..))")说明在调用com.bjsxt.service下面的包或其子包的add方法(任意参数)时调用下面的方法。
@Around("myMethod()")说明在myMethod()方法调用前运行一次下面的方法,在myMethod()方法调用结束后再调用一次下面的方法。
@Before("myMethod()")说明在myMethod()方法调用之前调用下面的方法。
spring_AOP_annotation的更多相关文章
- Spring AOP—注解配置方法的使用
Spring除了支持Schema方式配置AOP,还支持注解方式:使用@AspectJ风格的切面声明. 1 启用对@AspectJ的支持 Spring默认不支持@AspectJ风格的切面声明,为了支持需 ...
随机推荐
- http之cdn介绍
百度百科:CDN的全称是Content Delivery Network,即内容分发网络.CDN是构建在网络之上的内容分发网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡.内容分发.调度等功能 ...
- Java的家庭记账本程序(D)
日期:2019.2.8 博客期:031 星期一 今天是把程序的查询功能以列表的形式完成了! 截图如下:
- xilinx_all_version.lic
INCREMENT ISE_Vivado_Seth xilinxd -dec- uncounted \ C25FB036D304 VENDOR_STRING=License_Type:Bought H ...
- 三.linux磁盘与文件系统
第一层 机械硬盘 和 固态硬盘 结构 接口 机械硬盘stat.sas 固态pci-e .nvme也叫m2 硬盘的选择 磁盘内部组成 计算硬盘的大小 命令 fdisk -l 显示下面信息 大小=扇区大 ...
- 《剑指offer》 二进制中1的个数
本题来自<剑指offer> 二进制中1的个数 题目: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: 两种思路: 第一种:对n进行左移,检测最后一位是否为1,但考 ...
- python网络爬虫笔记(三)
一.切片和迭代 1.列表生成式 2.生成器的generate,但是generate保存的是算法,所以可以迭代计算,没有必要,每次调用generate 二.iteration 循环 1.凡是作用于for ...
- php 常用字符函数学习
1.addcslashes 要向字符串中的特定字符添加反斜杠 <?php header('Content-type:text/html;charset=utf8'); $str='are you ...
- FFmpeg的一般流程
FFMPeg一般流程: 1.av_register_all();//注册所有文件格式和编解码库 2.avformat_network_init();//打开网络视频流 3.av_open_input_ ...
- 刚发了两个关于极光推送的网上Demo,再次自己结合官网总结一下,以便加深印象
简单源码如下: //Map<String, String> parm是我自己传过来的参数,同学们可以自定义参数public static void jpushAndroid(Map< ...
- python获取信息
import uuid import socket def get_mac(): mac=uuid.UUID(int = uuid.getnode()).hex[-12:] return " ...