spring(五)---aspectj aop编程(xml配置)
2017-04-25 16:11
381 查看
一、简单应用
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 AOP+AspectJ在XML配置实例
- Intellij IDEA创建Spring项目之基于Aspectj的AOP操作(XML配置方式)
- Spring学习-22:Spring的AOP:基于AspectJ的XML配置方式开发
- Spring AOP AspectJ注解和XML配置两种实现(Maven构建)
- spring之aop编程——基于注解、xml配置文件方式
- Spring使用AspectJ注解和XML配置实现AOP
- spring通过xml方式配置AOP时,提示错误信息:匹配的通配符是严格的,但是对于元素'aop:aspectj- 自动代理”。
- Spring AOP+AspectJ在XML配置实例(详解)
- spring(六)---aspectj aop编程(注解配置)
- Spring之AOP使用xml配置更方便(使用AspectJ框架)(重点)
- Spring4.3.x 浅析xml配置的解析过程(10)——解析aop命名空间之aspectj-autoproxy标签
- Spring进阶之路(11)-使用Aspectj切面配置和XML配置文件方式实现切面编程
- Spring的AOP编程XML配置
- Spring3.0 学习-AOP面向切面编程_Spring AOP的XML配置模式
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring 【Aspectj】 pojo+xml 配置方式
- spring中配置<aop:aspectj-autoproxy/>与<mvc:annotation-driven/>作用以及国际化配置
- Spring中AOP基于XML的两种配置方法