aspectj xml
2024-09-08 07:16:05
1.接口和类
1.1 ISomeService 接口
public interface ISomeService { public void doSome(); public void dade();
}
1.2 SomeService类
public class SomeService implements ISomeService {
//核心业务
public void doSome(){
System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
} public void dade() {
//异常
//int sun=5/0;
System.out.println("++++++++++++++de+++++++++++++");
} }
1.3 MyAspect 类
public class MyAspect { //前置增强
public void before(){
System.out.println("=====before========");
}
//后置增强
public void afterReturing(){
System.out.println("=====after========");
}
//后置增强 目标方法的返回值
public void afterReturing(String result){
System.out.println("=====后置通知方法 result========"+result);
}
//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("目标方法执行前执行");
Object result = pjp.proceed();
System.out.println("目标方法执行后执行");
String temp=null;
if (result!=null) {
temp = (String) result;
temp=temp.toUpperCase();
}
return temp;
}
//异常通知
public void afterThrowing(){
System.out.println("异常通知");
}
//异常通知
public void afterThrowing(Exception ex){
System.out.println("异常通知 ex="+ex.getMessage());
} //最终通知
public void after(){
System.out.println("最终通知");
} }
2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--.目标对象-->
<bean id="someService" class="cn.bdqn.spring18.SomeService"></bean> <!--.增强类-->
<bean id="aspect" class="cn.bdqn.spring18.MyAspect"></bean> <!--aop-->
<aop:config>
<!--设置一个切点-->
<aop:pointcut id="mycut" expression="execution(* *..spring18.*.*(..))"></aop:pointcut>
<aop:aspect ref="aspect">
<aop:before method="before" pointcut-ref="mycut"></aop:before>
<aop:after-returning method="afterReturing" pointcut-ref="mycut"></aop:after-returning>
<aop:after-returning method="afterReturing(java.lang.String)" returning="result" pointcut-ref="mycut"></aop:after-returning>
<aop:around method="around" pointcut-ref="mycut"></aop:around>
<aop:after-throwing method="afterThrowing" pointcut-ref="mycut"></aop:after-throwing>
<aop:after-throwing method="afterThrowing(java.lang.Exception)" throwing="ex" pointcut-ref="mycut"></aop:after-throwing>
<aop:after method="after" pointcut-ref="mycut"></aop:after>
</aop:aspect>
</aop:config>
</beans>
3.测试类
//aspectj xml
@Test
public void test20(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring16.xml");
ISomeService service = (ISomeService) ctx.getBean("someService");
service.doSome();
service.dade();
}
最新文章
- ZeroMQ接口函数之 :zmq_sendmsg – 从一个socket上发送一个消息帧
- 牛顿法与拟牛顿法学习笔记(五)L-BFGS 算法
- c#.net循环将DataGridView中的数据赋值到Excel中,并设置样式
- PLSQL数据导入导出问题解决(空表、大字段表、表空间错误等)
- C#:Winform技巧
- 网页计算器,(类,隐藏域,style=display:block等)
- ctrlsf插件
- hdu2208之搜索
- poj 3370 鸽笼原理知识小结
- 区划代码 node 版爬虫尝试
- 201521123033《Java程序设计》第7周学习总结
- 如何从零开始对接第三方登录(Java版):QQ登录和微博登录
- ORA-01455
- JavaEESSM框架配置文件
- Zookeeper —— 一致性协议
- ansible hosts文件编写,简单使用测试(普通用户、sudo用户、root用户登录权限测试)
- OSINT系列:威胁信息挖掘ThreatMiner
- Codeforces 772A Voltage Keepsake - 二分答案
- css3种引入方式,样式与长度颜色,常用样式,css选择器
- InvalidateRect,invalidate,updatewindow(转)
热门文章
- C#继承与多态
- SPOJcot2 Count on a tree II (树上莫队)
- 获取当前正在运行的activity
- Run-Time Check Failure #3 - The variable &#39;p&#39; is being used without being initialized.
- 1、webpack课程介绍
- Attributes.Add用途与用法
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】
- HDU 3729【二分匹配】
- mongodb 由于计算机死机造成的无法启动故障
- [Xcode 实际操作]八、网络与多线程-(25)实现ShareSdk的社会化分享功能