spring(五)---aspectj aop编程(xml配置)
来源:互联网 发布:吉特巴舞迷彩服淘宝网 编辑:程序博客网 时间:2024/06/03 08:11
一、简单应用
1、建工程,导包
2、写目标对象
package top.einino.service;
public interface PersonService {
void eat();
}
package top.einino.service.impl;
import top.einino.service.PersonService;
public class PersonServiceImpl implements PersonService{
@Override
public void eat(){
System.out.println("吃饭!!");
//int i = 1/0;
}
}
3、写切面
package top.einino.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class PersonAspect {
//前置通知 参数为连接点对象,如果抛出异常,默认方法不能执行
public void before(JoinPoint joinPoint){
//连接点信息
System.out.println(joinPoint.toLongString());
System.out.println("前置通知:买菜");
}
//后置通知,第一个参数是连接点对象,第二个参数是方法返回值,参数名必须与配置文件的名字一样
public void afterReturning(JoinPoint joinPoint, Object result){
System.out.println("后置通知:目标返回值"+result);
}
//环绕通知,参数为可执行的连接点对象,可以实现任何通知效果
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕通知执行前:做饭");
Object result = proceedingJoinPoint.proceed();
System.out.println("环绕通知执行后:清洗");
}
//异常通知,第一个参数为连接点对象,第二个参数为异常对象
public void afterThrowing(JoinPoint joinPoint, Exception e){
//得到方法名
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法发生异常"+e);
}
//最终final通知,无论该方法是否出现异常,该方法总是会执行,被运用于释放资源
public void after(JoinPoint joinPoint){
System.out.println("结束整个流程");
}
}
4、写配置文件
<?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实例化 -->
<!-- 配置目标对象 -->
<bean id="personService" class="top.einino.service.impl.PersonServiceImpl"/>
<!-- 配置切面bean -->
<bean id="personAspect" class="top.einino.aspect.PersonAspect"/>
<!-- aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* top.einino.service.impl.PersonServiceImpl.*(..))" id="personPointcut"/>
<!-- ref定义切面 -->
<aop:aspect ref="personAspect">
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="personPointcut"/>
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="personPointcut"/>
<!-- 后置通知 returning值要与方法参数名一致-->
<aop:after-returning method="afterReturning" returning="result" pointcut-ref="personPointcut"/>
<!-- 异常抛出通知 -->
<aop:after-throwing method="afterThrowing" throwing="e" pointcut-ref="personPointcut"/>
<!-- 最终final通知 -->
<aop:after method="after" pointcut-ref="personPointcut"/>
</aop:aspect>
</aop:config>
</beans>
5、测试
package top.einino.junit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.einino.service.PersonService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestPersonAspect {
@Autowired
PersonService personService;
@Test
public void testEat(){
personService.eat();
}
}
二、小结
本博文简单介绍了aspectj aop的简单使用过程,AspectJ 是一个框架 (第三方AOP框架 ),提供切面编程 ,编写一个Aspect 支持多个Advice和多个PointCut 。在配置文件中,方法定义顺序不同,方法切入过程也会不同,像本博文的定义顺序,在不抛异常的情况下会依次执行before,around(方法执行前后),afterReturning,after;在抛异常的情况下会依次执行before,around(方法执行前),afterThrowing,after。
如果有疑问或者对该博文有何看法或建议或有问题的,欢迎评论,恳请指正!
- spring(五)---aspectj aop编程(xml配置)
- spring的AOP(五)----Spring AOP 编程(AspectJ )
- spring(六)---aspectj aop编程(注解配置)
- Spring详解(五)------AspectJ 实现AOP
- Spring详解(五)------AspectJ 实现AOP
- AspectJ实现AOP(xml配置方式)
- AOP(AspectJ、Spring AOP)
- Spring AOP+AspectJ在XML配置实例
- Spring之AOP使用xml配置更方便(使用AspectJ框架)(重点)
- Spring(十一)AspectJ框架开发AOP(基于xml)
- Spring AOP技术(基于AspectJ)的XML开发
- zz 基于@AspectJ配置Spring AOP(之二)
- Spring AOP实例(schema配置,aspectJ注解)
- Spring AOP切面编程 AspectJ
- Spring AOP与AspectJ 面向切面编程配置与注解
- 基于@AspectJ配置Spring AOP
- Spring Aspectj AOP配置学习
- Spring AOP Aspectj 注解配置
- 函数调用方式引起的编译错误
- Android MVP框架
- MDC介绍 -- 一种多线程下日志管理实践方式
- ThinkPHP的使用(八)发送邮箱
- ionic使用cordovaMedia、cordovaFile和cordovaFileTransfer播放在线音乐
- spring(五)---aspectj aop编程(xml配置)
- java中基本格式的校验
- Java高效编程之Builder模式
- Web安全之点击劫持(ClickJacking)
- mysql分表与分区的区别
- TCP演示
- 使用nc检测udp端口是否可以正常通讯 【转】
- redis-trib.rb命令详解
- FtpClient.storeFile返回false,本地测试可以,部署到服务器不可以