1:无参aop下面为项目结构

2:通知类。MyAdvice

package cn.edu.aop;

import org.aspectj.lang.ProceedingJoinPoint;

//通知类
public class MyAdvice {
//前置通知
public void before(){
System.out.println("before...");
}
//后置通知
public void after(){
System.out.println("after...");
}
//返回后通知
public void afterReturning(){
System.out.println("afterReturning...");
}
//抛出异常后通知
public void afterThrowing(){
System.out.println("afterThrowing...");
}
//环绕通知//特殊
public void around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("around before...");
//执行切入点方法
pjp.proceed();
System.out.println("around after...");
}
}

3:服务类。UserService

package cn.edu.aop;

public class UserService {
// 切入点
public void add() {
System.out.println("add");
// 制造出现异常
// int x = 1/0;
}
}

4:测试类。AOPAPP 

package cn.edu.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPAPP { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ctx.getBean("userService");
us.add();
} }

5:配置文件。applicationContext.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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 通知类 -->
<bean id="myAdvice" class="cn.edu.aop.MyAdvice"></bean>
<aop:config>
<aop:aspect ref="myAdvice">
<aop:pointcut expression="execution(* *..*.add())" id="pt" />
<!-- 各种切入类型 -->
<!--
<aop:before method="before" pointcut-ref="pt" />
<aop:after method="after" pointcut-ref="pt"/>
<aop:after-returning method="afterReturning" pointcut-ref="pt"/> <aop:after-throwing method="afterThrowing" pointcut-ref="pt"/>
<aop:around method="around" pointcut-ref="pt"/> --> </aop:aspect>
</aop:config> <bean id="userService" class="cn.edu.aop.UserService"></bean>
</beans>

--------------------------------------------------以上为无参AOP----------------------------------------------

--------------------------------------------------以下为有参AOP----------------------------------------------

//通知类
public class MyAdvice {
// 前置通知
public void before(JoinPoint jp) {
System.out.println("before...");
// 获取切入点方法参数
Object[] objs = jp.getArgs();
System.out.println("切入点方法参数:" + objs[0] + "," + objs[1]);
} // 后置通知
public void after(JoinPoint jp) {
System.out.println("after...");
} // 返回后通知
public void afterReturning(JoinPoint jp, Object asd) {
System.out.println("afterReturning...");
// 输出切入点方法的返回值
System.out.println(asd+"123");
} // 抛出异常后通知
public void afterThrowing() {
System.out.println("afterThrowing...");
} // 环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("around before...");
//获取切入点方法参数
Object[] objs = pjp.getArgs();
//去掉输入字符串参数前后的空格
objs[0] = objs[0].toString().trim();
System.out.println("切入点方法参数:"+objs[0]+","+objs[1]);
//执行切入点方法
Object result = pjp.proceed(objs);
//输出切入点方法的返回值
System.out.println(result);
System.out.println("around after...");
return 200;
} }
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="myAdvice" class="cn.edu.aop.MyAdvice"></bean>
<aop:config>
<aop:aspect ref="myAdvice">
<aop:pointcut expression="execution(* *..*.add(..))" id="pt"/>
<!-- <aop:before method="before" pointcut-ref="pt"/> -->
<aop:after-returning method="afterReturning" pointcut-ref="pt" returning="asd"/>
<!-- <aop:around method="around" pointcut-ref="pt"/> -->
</aop:aspect>
</aop:config>
<bean id="userService" class="cn.edu.aop.UserService"></bean>
</beans>
public class UserService {

    public int add(String a, int b) {
System.err.println("add");
System.out.println("add方法的输入参数" + a + "," + b);
return 2017;
} }
public class AOPAPP {

    public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ctx.getBean("userService");
int r=us.add("wowokkk", 100);
System.out.println("add方法返回值APP"+r);
} }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

spring基础学习---aop的更多相关文章

  1. Spring基础系列--AOP织入逻辑跟踪

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...

  2. Spring基础系列-AOP源码分析

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...

  3. Spring基础学习,附例子代码讲解

    什么是Spring.IOC.AOP.DI?     Spring是一个基于IOC和AOP的结构J2EE系统的框架.     IOC(Inversion Of Control)控制反转(Spring的基 ...

  4. spring基础学习01

    spring基础 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用 IOC控制反转 把创建对象和维护对象之间的关系权利 ...

  5. Spring Boot学习——AOP编程的简单实现

    首先应该明白一点,AOP是一种编程范式,是一种程序设计思想,与具体的计算机编程语言无关,所以不止是Java,像.Net等其他编程语言也有AOP的实现方式.AOP的思想理念就是将通用逻辑从业务逻辑中分离 ...

  6. Spring基础学习(四)—AOP

    一.AOP基础 1.基本需求      需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...

  7. 【spring基础】AOP概念与动态代理详解

    一.代理模式 代理模式的英文叫做Proxy或Surrogate,中文都可译为”代理“,所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一 ...

  8. spring基础概念AOP与动态代理理解

    一.代理模式 代理模式的英文叫做Proxy或Surrogate,中文都可译为”代理“,所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一 ...

  9. Spring基础20——AOP基础

    1.什么是AOP AOP(Aspect-Oriented Programming)即面向切面编程,是一种新的方法论,是对那个传统OOP面向对象编程的补充.AOP的主要编程对象是切面(aspect),而 ...

随机推荐

  1. oracle打开或者关闭flashback

    1.打开flashback: 关闭数据库 SQL>shutdown immediate; 启动到mount方式 SQL>startup mount; 如果归档没有打开,打开归档[因为fla ...

  2. 初遇Java

    什么是JVM?JVM是java虚拟机(JVM Java Virtual Machine),java程序需要运行在虚拟机上,不同平台有自己的虚拟机,因此java语言可以跨平台. 什么是JRE?包括Jav ...

  3. 1007 Maximum Subsequence Sum (PAT(Advance))

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  4. 【Codeforces 501C】Misha and Forest

    [链接] 我是链接,点我呀:) [题意] 给你一棵树 但是每个节点只告诉你出度个数 以及所有和它相连的点的异或和. 让你还原这棵树 [题解] 叶子节点的话,他所有节点的异或和就是它那唯一的一个爸爸 因 ...

  5. JRebel 7.1.5 插件下载 安装 激活 结合 IntelliJ IDEA--自动编译进行热部署---

    Intellij IDEA 安装和配置jrebel进行项目的热部署 https://www.cnblogs.com/a8457013/p/7866625.html Intellij IDEA 使用jr ...

  6. c++ 上机实验题

    c++语言俺是不会啦,但是朋友考试需要,那只能勉为其难的入门下做做考试题了. 以下就是具体的题目和答案: ----------------------------------------------- ...

  7. [Codeforces 876]比赛记录

    上场$rating$果然炸飞,但是据说这次只要不$FST$就能翻回来QWQ? T1  $dfs$乱搞? T2  取模乱搞,$STL$ $vector$大法好(%%%$ryf$秒出做法) T3  看了半 ...

  8. - > 并查集模板

    思路:在博客园里,有对并查集思路的详解,模板神马的只是饭后甜点: 这儿有只野生模板君飘过,请各位OIer尽快捕捉 #include<iostream> #include<cstdio ...

  9. Java中原始数据类型存放位置理解

    原始数据类型的变量存放在栈还是堆,应该由上下文去决定. 如下所示的局部方法中,定义了本地变量a,且为原始数据类型,所以存放在栈中. public void func(){ int a = 3; } 再 ...

  10. ubuntu Change Language

    慎用 所謂的語系(locale),其實包含語言及地區的設定,因為除了語言之外,國家與國家的日期表示方式.數字格式.貨幣符號.度量單位可能都不一樣,所以才會有了這個locale的發明,依照國家和語言給予 ...