spring基础学习---aop
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的更多相关文章
- Spring基础系列--AOP织入逻辑跟踪
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...
- Spring基础系列-AOP源码分析
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...
- Spring基础学习,附例子代码讲解
什么是Spring.IOC.AOP.DI? Spring是一个基于IOC和AOP的结构J2EE系统的框架. IOC(Inversion Of Control)控制反转(Spring的基 ...
- spring基础学习01
spring基础 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用 IOC控制反转 把创建对象和维护对象之间的关系权利 ...
- Spring Boot学习——AOP编程的简单实现
首先应该明白一点,AOP是一种编程范式,是一种程序设计思想,与具体的计算机编程语言无关,所以不止是Java,像.Net等其他编程语言也有AOP的实现方式.AOP的思想理念就是将通用逻辑从业务逻辑中分离 ...
- Spring基础学习(四)—AOP
一.AOP基础 1.基本需求 需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...
- 【spring基础】AOP概念与动态代理详解
一.代理模式 代理模式的英文叫做Proxy或Surrogate,中文都可译为”代理“,所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一 ...
- spring基础概念AOP与动态代理理解
一.代理模式 代理模式的英文叫做Proxy或Surrogate,中文都可译为”代理“,所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一 ...
- Spring基础20——AOP基础
1.什么是AOP AOP(Aspect-Oriented Programming)即面向切面编程,是一种新的方法论,是对那个传统OOP面向对象编程的补充.AOP的主要编程对象是切面(aspect),而 ...
随机推荐
- 考试总结(CE???)
直接开写题解: (由于T1为暴力模拟,不进行整理) T2: 扶苏给了你一棵树,这棵树上长满了幼嫩的新叶,我们约定这棵树的根是 1,每个节 点都代表树上的一个叶子. 如果你不知道什么叫树,你可以认为树是 ...
- try catch影响Spring事务吗?
对于这个问题有两种情况: 1.catch只打印异常,不抛出异常 try { 数据库做添加订单表; /; 数据库减少库存; }catch (Exception e){ e.printStackTrace ...
- python下载网络文件
python下载网络文件 制作人:全心全意 下载图片 #!/usr/bin/python #-*- coding: utf-8 -*- import requests url = "http ...
- buf.writeUIntBE()函数详解
buf.writeUIntBE(value, offset, byteLength[, noAssert]) buf.writeUIntLE(value, offset, byteLength[, n ...
- 返回通知&异常通知&环绕通知
[返回通知] LoggingAspect.java: @Aspect @Component public class LoggingAspect { /* * 在方法正常执行后执行的通知叫返回通知 * ...
- TensorFlow Ops
TensorFlow Ops 1. Fun with TensorBoard In TensorFlow, you collectively call constants, variables, op ...
- HDU 1212 大整数的取模运算
因为这里是MOD最大为100000 所以我将字符串看作5个一组,并记录后面跟了多少个100000 每次取5个数根据其数据进行取模更新 注意过程中 100000*100000会超int #include ...
- HDU 1042 大数计算
这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就MLE 这里写好大数后,每次输入一个n,然后再老老实实一个个求阶层就好 java代码: /** * @(#)Main. ...
- codeforces 371B - Fox Dividing Cheese
#include<stdio.h> int count; int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); ...
- 矩形面积求并(codevs 3044)
题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...