配置文件

<?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的简单示例的更多相关文章

  1. Spring Boot 2.X(八):Spring AOP 实现简单的日志切面

    AOP 1.什么是 AOP ? AOP 的全称为 Aspect Oriented Programming,译为面向切面编程,是通过预编译方式和运行期动态代理实现核心业务逻辑之外的横切行为的统一维护的一 ...

  2. c# spring aop的简单例子

    刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...

  3. java代理课程测试 spring AOP代理简单测试

    jjava加强课程测试代码 反射. 代理 .泛型.beanUtils等 项目源码下载:http://download.csdn.net/detail/liangrui1988/6568169 热身运动 ...

  4. Spring AOP配置简单记录(注解及xml配置方式)

    在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...

  5. spring aop expression简单说明

    <aop:config> <aop:pointcut id="userDAO" expression="execution(public * cn.da ...

  6. spring aop介绍和示例

    参考:<Spring in Action> 一.AOP介绍 AOP是Aspect Oriented Programming的缩写,意思是面向切面编程. 应用中有一些功能使用非常普遍,比如事 ...

  7. spring 理解Spring AOP 一个简单的约定游戏

    应该说AOP原理是Spring技术中最难理解的一个部分,而这个约定游戏也许会给你很多的帮助,通过这个约定游戏,就可以理解Spring AOP的含义和实现方法,也能帮助读者更好地运用Spring AOP ...

  8. spring boot thymeleaf简单示例

    说实话,用起来很难受,但是人家官方推荐,咱得学 如果打成jar,这个就合适了,jsp需要容器支持 引入依赖 <dependency> <groupId>org.springfr ...

  9. JAVA入门[20]-Spring Data JPA简单示例

    Spring 对 JPA 的支持已经非常强大,开发者只需关心核心业务逻辑的实现代码,无需过多关注 EntityManager 的创建.事务处理等 JPA 相关的处理.Spring Data JPA更是 ...

随机推荐

  1. C#_02.15_基础六_.NET类

    C#_02.15_基础六_.NET类 一.类继承是一个类在另一个类的基础上进行的扩展. 继承的子类拥有父类的全部成员.索引子类拥有本身的全部成员以及父类的全部成员. 可以对基类成员进行隐藏,如果必须的 ...

  2. 35、concurrent.futures模块与协程

    concurrent.futures  —Launching parallel tasks    concurrent.futures模块同时提供了进程池和线程池,它是将来的使用趋势,同样我们之前学习 ...

  3. 更新 app 操作过期提示

    增加的操作过期提示,再让用户选择是否登录,登录成功后自动返回上一次操作的面页

  4. bat获取文件夹里面所有文件夹的名称方法

    创建一个123.txt文档,修改名称为123.bat 里面填写内容如下: DIR *.*  /B >文件名清单.TXT 保存,双击执行即可获取生成文件夹名称的txt文档

  5. 在python里调用java的py4j的使用方法

    py4j可以使python和java互调 py4j并不会开启jvm,需要先启动jvm server,然后再使用python的client去连接jvm GatewayServer实例:允许python程 ...

  6. 终止java线程的2种方法

    1.使用一个volatile的共享变量 2.使用interrupt方法 import java.util.concurrent.TimeUnit; /** * ThreadTest */ public ...

  7. 记录一次使用VS2015编译错误的原因查找(boost+gdal)

    编译错误说明 在一个解决方案中的项目A中使用到了boost,完全没有问题.在项目B中也使用了boost库,编译的时候就产生了一堆错误. 原因查找 两个项目通过对比,唯一的不同就是项目B使用了gdal库 ...

  8. 分析轮子(九)- Cloneable.java

    注:玩的是JDK1.7版本 一:Cloneable.java 接口也是标记接口,所以,它没有任何方法和属性,实现此接口表示的意思是:可以调用 Object.java 类的 clone() 方法,进行简 ...

  9. TensorFlow精选Github开源项目

    转载于:http://www.matools.com/blog/1801988 TensorFlow源码 https://github.com/tensorflow/tensorflow 基于Tens ...

  10. Atitit s2018.6 s6 doc list on com pc.docx Atitit s2018.6 s6 doc list on com pc.docx  Aitit algo fix 算法系列补充.docx Atiitt 兼容性提示的艺术 attilax总结.docx Atitit 应用程序容器化总结 v2 s66.docx Atitit file cms api

    Atitit s2018.6 s6  doc list on com pc.docx Atitit s2018.6 s6  doc list on com pc.docx  Aitit algo fi ...