spring基础学习---aop
2024-09-06 17:33:45
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);
} }
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
最新文章
- 一个Angular模块中可以声明哪些组件?
- JAVA RMI例子
- 记一次串口通信调试,慎用SerialPort.Close
- Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法
- ECMAscript v.s. Javascript
- c、c++知识点
- linux内核--内存管理(二)
- POJ 1579-Function Run Fun(内存搜索)
- python爬虫-知乎登录
- Silverlight程序中访问配置文件
- 一道有趣的Twitter技术面试题
- centos7 mysql-server 安装过程
- 新建 .NET Core 控制台项目
- IIS 反向代理设置
- [转]DevOps的三大原则
- Spark 集群管理命令
- dede:channel的type改为son,currentstyle当前样式就不起作用
- js基础学习笔记(四)
- html5shiv 是一个针对 IE 浏览器的 HTML5 JavaScript 补丁,目的是让 IE 识别并支持 HTML5 元素。
- css引用方式
热门文章
- NOIP2000方格取数(洛谷,动态规划递推)
- 爬虫之Re库
- 3. Python中的分支判断、循环
- NOIP2018提高组省一冲奖班模测训练(一)
- [ural1057][Amount of Degrees] (数位dp+进制模型)
- Webstorm如何配置自动补全前缀--autoprefixer
- POJ 2217 Secretary
- [luoguP2915] [USACO08NOV]奶牛混合起来Mixed Up Cows(DP)
- 如何将jsp后缀重写为html
- A+B Problem IV