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将缓存其返回 ...
随机推荐
- ecshop数据结构
ecshop数据结构 2.7.2版本,数据库表 共88张表 注: 1.颜色为蓝色的字,有待讨论验证的地方. 2.颜色为红色的字,是新增的字段.(改文档是基于网上下载的老版本的数据字典修改而成,已经检 ...
- 来吧,一文彻底搞懂Java中的Comparable和Comparator
大家好,我是沉默王二,今天在逛 programcreek 的时候,我发现了一些专注细节但价值连城的主题.比如说:Java 的 Comparable 和 Comparator 是兄弟俩吗?像这类灵魂拷问 ...
- 搭建nginx
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- ffmpeg 视频合并
/// <summary> /// 视频合并 /// </summary> /// <param name="File1">第一个视频地址< ...
- opensuse安装Tomcat碰到的问题
已经安装好JDE,并配置好环境变量 从官网下载Tomcat tar包,解压到用户目录,进入运行bin下的start.sh,显示运行成功,但是浏览器中输入localhost:8080连接不上 检查一番发 ...
- C# 中 ConfigureAwait 相关答疑FAQ
C# 中 ConfigureAwait 相关答疑FAQ 在前段时间经常看到园子里有一些文章讨论到 ConfigureAwait,刚好今天在微软官方博客看到了 Stephen Toub 前不久的一篇答疑 ...
- C++ 排序引用的优化
链接:https://www.nowcoder.com/acm/contest/83/B来源:牛客网 题目描述 第一次期中考终于结束啦!沃老师是个语文老师,他在评学生的作文成绩时,给每位学生的分数都是 ...
- 假期续命充电——简单上手tesorflow2 框架
谈到深度学习,就不得不谈到tensorflow 在tensorflow之后出了2.0版本,相比之前有了很大的改变,趁着假期赶紧冲冲冲! 稍微学习了一些基础,做一个自我总结,作为一些基础的知识不再过多重 ...
- ASP.NET 开源导入导出库Magicodes.IE 导出Pdf教程
基础教程之导出Pdf收据 说明 本教程主要说明如何使用Magicodes.IE.Pdf完成Pdf收据导出 要点 导出PDF数据 自定义PDF模板 导出单据 如何批量导出单据 导出特性 PdfExpor ...
- Ubuntu阿里镜像
ubuntu 14.04: http://mirrors.aliyun.com/ubuntu-releases/14.04/ ubuntu 16.04: http://mirrors.aliyun.c ...