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将缓存其返回 ...
随机推荐
- Linux常用命令大全(一)
Linux常用命令大全(一) 第一章 cal命令 $ cal 12 2017 :列出2017年12月的日历 $ cal 10 :列出公元10年的日历 $ cal 12 17 :列出公元17年12月的日 ...
- 【5min+】 什么?原来C#还有这两个关键字
系列介绍 简介 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的. ...
- 1059 C语言竞赛 (20 分)C语言
C 语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛.既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽: 0.冠军将赢得一份"神秘大奖"(比如很巨大的一本学生研究论文集--). 1 ...
- Spring中常见的设计模式——策略模式
策略模式(Strategy Pattern) 一.策略模式的应用场景 策略模式的应用场景如下: 系统中有很多类,而他们的区别仅仅在于行为不同. 一个系统需要动态的在集中算法中选择一种 二.用策略模式实 ...
- [Windows10]记一次修复注册表相关血案:该文件没有与之关联的应用来执行该操作。请安装应用,若已经安装应用,请在“默认应用设置”页面中创建关联。
今天闲得蛋疼清理了一下右键菜单,于是在之后某时刻使用Everything的“双击路径列打开目录”功能时发现异常: [Window Title] Everything.exe [Content] 该文件 ...
- VScode(一):C/C++ & MinGW & Code Runner
目录 1 VScode配置安装 2 MinGW配置安装 2.1 MinGW下载安装 2.2 MinGW环境配置 3 VScode编译C/C++ 3.1 扩展插件安装 3.2 项目配置 3.2.1 配置 ...
- 【转】21个免费的UI界面设计工具、资源及网站
本文将介绍21个免费的UI界面设计工具.资源及网站,如果你在做用户体验设计.界面设计.产品设计.JS前段开发.手机产品设计以及iPad和平板电脑产品设计,不妨来看看. AD: 2013云计算架构师峰会 ...
- 小白学Java:老师!泛型我懂了!
目录 小白学Java:老师!泛型我懂了! 泛型概述 定义泛型 泛型类的定义 泛型方法的定义 类型变量的限定 原生类型与向后兼容 通配泛型 非受限通配 受限通配 下限通配 泛型的擦除和限制 类型擦除 类 ...
- try catch 自定义捕获异常
当我们完成一个程序时,如果没有异常捕获的话,用户使用时会出现许多bug.而加入异常捕获之后便会提醒用户使用时避免产生不必要的错误.具体操作实现如下: 首先创造一个MyException类,继承自Exc ...
- cannot insert multiple commands into a prepared statement问题原因及解决办法
问题是这样,我在对数据库进行写操作(添加.删除.修改)时,我想同时删除两个表中的两条关联数据,像这样 let sql = ` DELETE FROM bridge_parts WHERE id = $ ...