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. C++ Primer(第4版)-学习笔记-第4部分:面向对象编程与泛型编程

    第15章 面向对象编程OOP(Object-oriented programming)           面向对象编程基于三个基本概念:数据抽象.继承和动态绑定.      在 C++ 中,用类进行 ...

  2. HDU - 6264 - Super-palindrome(思维)

    题意: 给出一个字符串,使得所有的奇数连续子串为回文串,输出最小变化次数 思路: 分析过后,只存在两种情况,1全部为一种字母,2形如abab交替类型 对于奇数位和偶数位单独计数,只需计算出奇数位或者偶 ...

  3. Ice Cream Tower(The 2016 ACM-ICPC Asia China-Final Contest 二分&贪心)

    题目: Mr. Panda likes ice cream very much especially the ice cream tower. An ice cream tower consists ...

  4. SQL Server 机考,用T-SQL编写 简单实例

    使用T-SQL实现以下要求: 要求如下: 1,添加数据库:MySchool 2,添加学生基础表:Student 3,添加学生成绩表:ScoreInfo 4,两张表结构分别如下 Student表结构:( ...

  5. E - Cricket Field

    Description   Once upon a time there was a greedy King who ordered his chief Architect to build a fi ...

  6. 关于pom.xml中的dependency中的顺序

    特别注意: <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>l ...

  7. IE 浏览器证书错误常见问题解答

    有时,你将看到下表的错误消息,比如,此网站的安全证书已经被吊销等等,说明网站的安全证书有问题. 站点的证书允许 Internet Explorer 与站点建立安全连接. SSL证书出现错误有时是证书本 ...

  8. 【Codeforces 264B】Good Sequences

    [链接] 我是链接,点我呀:) [题意] 让你在一个递增数组中选择一个最长子序列使得gcd(a[i],a[i+1])>1 [题解] 设f[i]表示以一个"含有素因子i的数字" ...

  9. Java基础学习总结(83)——Java泛型总结

    1. 什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型 ...

  10. Leetcode 123.买卖股票的最佳时机III

    买卖股票的最佳时机III 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你 ...