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),而 ...
随机推荐
- HDU_5783_DivideTheSequence
HDU_5783_DivideTheSequence 点击打开链接 题意: 生成尽量多的连续的子串,且子串的前缀和大于等于0,输出符合题意的子串的数量. 这题目是参加四月份的个人训练赛遇到的,挺水的 ...
- centos7修改时间和时区
设置时区同样, 在 CentOS 7 中, 引入了一个叫 timedatectl 的设置设置程序. 用法很简单: # timedatectl # 查看系统时间方面的各种状态 Local time: 四 ...
- 二: 安装centos服务环境软件mysql httpd php
安装mysql--------------------------------------wget http://dev.mysql.com/get/mysql-community-release-e ...
- PyCharm开发GUI之PyQt安装
开发环境 PyCharm 2018.3.3python3.7 1 安装pyqt5 pip install PyQt5-tools 2 配置PyCharm 2.1 配置设计器 其中,program为C: ...
- Nginx学习总结(3)——Nginx配置及应用场景之高级配置
一.Nginx反向代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器:并将从服务器上得到的结果返回给Internet ...
- [luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)
传送门 有向图,找点数大于1的强连通分量个数 ——代码 #include <stack> #include <cstdio> #include <cstring> ...
- sdibt 1244类似于拓扑排序
博客:http://blog.csdn.net/mypsq/article/details/39005991 #include<stdio.h> #include<string.h& ...
- 使用ajax,后台传回的数据处理
使用ajax,后台传到前台的是json数据,但这只是一个字符串的形式,需要进行转换为对象 转换后再使用$.each()方法进行遍历 使用alert(typeof msg) ; 可以查看msg的类型 ...
- 临时起异,要进入C++领域耍一个程序
没办法.两周之内可以搞定吧. 就一个SESSION 0的问题. 网上有类似源码,调一下应该就可以吧..保佑顺利. 基本语法都还记得,快N年啦... #include <iostream> ...
- Just a Hook 线段树 区间更新
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...