Spring AOP的简单示例
配置文件
<?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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
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-3.0.xsd"> <!-- 激活组件扫描功能,在包cn.ysh.studio.spring.aop及其子包下面自动扫描通过注解配置的组件 -->
<context:component-scan base-package="com.zhiguoguo.service,aspect"/> <!-- 激活自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
service类:
package com.zhiguoguo.service; import org.springframework.stereotype.Component; @Component
public class HelloService {
public String sayHello() {
System.out.println("——————方法执行——————");
// throw new RuntimeException("haha");
return "Hello world!";
}
}
切面AOP:
package com.zhiguoguo.aspect; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; /**
* 定义切面
*/
@Component
@Aspect
public class HelloServiceAspect { @Before("execution(* com.zhiguoguo.service.HelloService.*(..))")
public void authorith(){
System.out.println("模拟进行权限检查。");
} @After("execution(* com.zhiguoguo.service.HelloService.*(..))")
public void release() {
System.out.println("模拟方法结束后的释放资源...");
System.out.println();
} @AfterReturning(returning="obj", pointcut="execution(* com.zhiguoguo.service.HelloService.*(..))")
public void log(Object obj) {
System.out.println("模拟目标方法返回值:" + obj);
System.out.println("模拟记录日志功能...");
System.out.println();
} @AfterThrowing(throwing="ex", pointcut="execution(* com.zhiguoguo.service.HelloService.*(..))")
public void doRecoverActions(Throwable ex) {
System.out.println("目标方法中抛出的异常:" + ex);
System.out.println("模拟抛出异常后的增强处理...");
} // @Around("execution(* com.zhiguoguo.service.HelloService.*(..))")//这里先注释掉
public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable {
System.out.println("执行目标方法之前,模拟开始事物...");
// 执行目标方法,并保存目标方法执行后的返回值
Object rvt = jp.proceed(); //这里的切面方法如果有参数才能传递一个参数给他
// Object rvt = jp.proceed(new String[]{"被改变的参数"}); System.out.println("执行目标方法之前,模拟结束事物...");
System.out.println(); //经过这么一个环绕,可以增强返回值
return rvt + "新增的内容";
} }
主函数:
package main; import com.zhiguoguo.service.HelloService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
HelloService helloService = context.getBean(HelloService.class);
helloService.sayHello();
}
}
Spring AOP的简单示例的更多相关文章
- Spring Boot 2.X(八):Spring AOP 实现简单的日志切面
AOP 1.什么是 AOP ? AOP 的全称为 Aspect Oriented Programming,译为面向切面编程,是通过预编译方式和运行期动态代理实现核心业务逻辑之外的横切行为的统一维护的一 ...
- c# spring aop的简单例子
刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...
- java代理课程测试 spring AOP代理简单测试
jjava加强课程测试代码 反射. 代理 .泛型.beanUtils等 项目源码下载:http://download.csdn.net/detail/liangrui1988/6568169 热身运动 ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...
- spring aop expression简单说明
<aop:config> <aop:pointcut id="userDAO" expression="execution(public * cn.da ...
- spring aop介绍和示例
参考:<Spring in Action> 一.AOP介绍 AOP是Aspect Oriented Programming的缩写,意思是面向切面编程. 应用中有一些功能使用非常普遍,比如事 ...
- spring 理解Spring AOP 一个简单的约定游戏
应该说AOP原理是Spring技术中最难理解的一个部分,而这个约定游戏也许会给你很多的帮助,通过这个约定游戏,就可以理解Spring AOP的含义和实现方法,也能帮助读者更好地运用Spring AOP ...
- spring boot thymeleaf简单示例
说实话,用起来很难受,但是人家官方推荐,咱得学 如果打成jar,这个就合适了,jsp需要容器支持 引入依赖 <dependency> <groupId>org.springfr ...
- JAVA入门[20]-Spring Data JPA简单示例
Spring 对 JPA 的支持已经非常强大,开发者只需关心核心业务逻辑的实现代码,无需过多关注 EntityManager 的创建.事务处理等 JPA 相关的处理.Spring Data JPA更是 ...
随机推荐
- set non-stop on
# Enable the async interface. set target-async 1 # If using the CLI, pagination breaks non-stop. set ...
- Win10注册表无法保存对权限所作的更改拒绝访问
在对系统的安全控制得越来越多的情况下,要对注册表的关键数据进行修改是件挺麻烦的事,时不时会弹出无法保存对xxxxxx权限所作的更改,拒绝访问,操作产生错误,操作出现错误的提示,这时怎么办呢?这里就最近 ...
- HEVC与VP9之间的对比
在streamingmedia上看到的一篇对比HEVC与VP9的文章,挺不错.另外这边文章的几个comment也是不错的. 下面是全文. The Great UHD Codec Debate: G ...
- 每位 Ubuntu 18.04 用户都应该知道的快捷键 | Linux 中国
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/82879369 wx_fmt=jpeg& ...
- Javascript 生成随机数
Math.random().toString(16).substring(2) // 固定13位 Math.random().toString(36).substring(2)//倍数不固定 22-2 ...
- mysql忘记密码时如何修改密码
1.首先关闭mysql服务 2.进入mysql安装目录,我的是在C:\Program Files\MySQL\MySQL Server 5.5\bin 3.dos命令行执行:mysqld -nt -- ...
- docker pureftpd
pureftpd: image: vimagick/pure-ftpd ports: - "21:21" volumes: - ./data/ftpuser:/home/ftpus ...
- iframe之间操作记录
1.watch.js (function ($) { $.fn.watch = function (callback) { return this.each(function () { //缓存以前的 ...
- (原)tensorflow使用eager在mnist上训练的简单例子
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9989586.html 代码网址: https://github.com/darkknightzh/t ...
- 开源自己写的Library到github,让别人或自己的项目依赖
对于不会git命令的自己,要上传项目或libary,看了本文,傻瓜式操作,绝壁简单! 新建一个空白工程 File-->New-->New module-->Android Libra ...