Spring基于注解配置AOP
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置自动扫描的包-->
<context:component-scan base-package="com.xiya.spring.aop"/>
<!--使AspectJ注释起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy/>
</beans>
<context:component-scan base-package="com.xiya.spring.aop"/>
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,
如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
com/xiya/spring/aop/advice/LoggingAspect.java
package com.xiya.spring.aop.advice; /**
* Created by N3verL4nd on 2017/3/24.
*/ import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; import java.util.Arrays;
import java.util.List; /**
* 如何把这个类声明为一个切面:
* 1、把该类放入IOC容器中
* 2、再声明为一个切面
*/
@Aspect
@Component
public class LoggingAspect {
//声明该方法是一个前置通知:在目标方法执行之前执行
@Before("execution(public int com.xiya.spring.aop.service.impl.ArithmeticCalculatorImpl.*(int,int))")
public void doBefore(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " begins with " + args); } //后置通知:在目标方法执行后(无论是否发生异常)执行的通知
//在后置通知中还不能访问目标方法执行的结果
@After("execution(* com.xiya.spring.aop.service.impl.ArithmeticCalculatorImpl.*(int,int))")
public void doAfter(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("the method " + methodName + " ends!");
}
}
com/xiya/spring/aop/service/ArithmeticCalculator.java
package com.xiya.spring.aop.service; /**
* Created by N3verL4nd on 2017/3/24.
*/
public interface ArithmeticCalculator {
int add(int x, int y);
int sub(int x, int y);
int mul(int x, int y);
int div(int x, int y);
}
com/xiya/spring/aop/service/impl/ArithmeticCalculatorImpl.java
package com.xiya.spring.aop.service.impl; import com.xiya.spring.aop.service.ArithmeticCalculator;
import org.springframework.stereotype.Component; /**
* Created by N3verL4nd on 2017/3/24.
*/
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int x, int y) {
int result = x + y;
//int z = 1 / 0;
return result;
} @Override
public int sub(int x, int y) {
int result = x - y;
return result;
} @Override
public int mul(int x, int y) {
int result = x * y;
return result;
} @Override
public int div(int x, int y) {
int result = x / y;
return result;
}
}
com/xiya/spring/aop/test/Main.java
package com.xiya.spring.aop.test; import com.xiya.spring.aop.service.ArithmeticCalculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by N3verL4nd on 2017/3/24.
*
*/
public class Main {
public static void main(String[] args) {
//1、创建Spring的IOC容器
ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml"); //2、从IOC容器中获取bean实例
ArithmeticCalculator calculator = context.getBean(ArithmeticCalculator.class); //3、使用bean
int result = calculator.add(10, 20);
System.out.println("result = " + result); result = calculator.div(10, 2);
System.out.println("result = " + result);
}
}
Spring基于注解配置AOP的更多相关文章
- 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置
复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...
- Spring 基于注解的AOP实现
在本文开始之前,我要引入一张图,这张图的来源 https://blog.csdn.net/chenyao1994/article/details/79708496 ,版权归原作者所有,我借鉴了原作者的 ...
- Spring基于XML配置AOP
目录结构: D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\StudentService.java pac ...
- spring-AOP框架(基于AspectJ注解配置AOP)
基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- Spring注解配置Aop
之前学习了SpringAop的基本原理.http://www.cnblogs.com/expiator/p/7977975.html 现在尝试使用注解来配置SpringAop. Aop,面向切面编程. ...
- Spring 基于 AspectJ 的 AOP 开发
Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...
- Spring基于注解的Cache支持
Spring为我们提供了几个注解来支持Spring Cache.其核心主要是@Cacheable和@CacheEvict.使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回 ...
随机推荐
- HDFS的HA集群原理分析
1.简单hdfs集群中存在的问题 不能存在两个NameNode 单节点问题 单节点故障转移 2.解决单节点问题 找额外一个NameNode备份原有的数据 会出现脑裂 脑裂:一个集群中多个管理者数据 ...
- 洛谷P2657 [SCOI2009]windy数 题解 数位DP
题目链接:https://www.luogu.com.cn/problem/P2657 题目大意:找区间 \([A,B]\) 范围内 不含前导零 且 相邻两个数字之差至少为2 的正整数的个数. 题目分 ...
- (httpd、php)
(一)http协议介绍 http: 超文本传输协议,http协议是应用层协议,实现http协议的软件都监听的TCP的80端口之上.http协议也是一种文本协议,是基于TCP协议实现 http协议有几个 ...
- 如何修改Docker已运行实例的端口映射
如何修改Docker已运行实例的端口映射 Docker的端口映射,往往出现在两个阶段需要处理: 1.是在docker启动前就已经确定好,哪个docker实例映射哪个端口(往往这个情况比较,需要提前做规 ...
- fastjson使用详解
目录 二.fastjson使用 三.fastjson 常用 API 四.fastjson使用演示 测试类准备 1.java类转换为json字符串 2.json字符串转为java类 五.fastjson ...
- 记录这两年是如何一步一步转型到.net core+k8s
2017年12月份,我离开北京,回到了武汉,开始在现在这家公司担任架构师工作.经过2年的时间,逐步完成以.net core+k8s为核心的技术架构.文末有彩蛋. 以下整理这两年的主要时间节点: 201 ...
- ArcEngine 里面的日期
问题: 将自己做的GIS系统放到其他系统上的时候发现用 IQueryFilter 进行时间查询的时候报错,原来的系统没有这个问题. 原因: 后来调试代码发现查询的时间里面有中文,显示格式 " ...
- Jquery图片上传功能整理
最近在做一个图片上传到服务器的功能,之前基本没有什么JS的经验,用的也是网上的插件.做了一个星期才把他弄好,现在做一下总结,方便以后查看. 用的插件是WebUploader,上面有很多例子,我找的例子 ...
- python列表(数组)
列表(list) 就是 数组 - 列表是Python中的一个对象 - 对象(object)就是内存中专门用来存储数据的一块区域 - 之前我们学习的对象,像数值,它只能保存一个单一的数据 - 列表中可 ...
- CentOS7 搭建Fabric 1.0
1.环境搭建 1.1 go的按装及配置 1.1.1下载go压缩包 wget https://dl.google.com/go/go1.9.2.linux-amd64.tar.gz 1.1.2 解压 ...