实验6---SpringAOP-XML+注解实现
需要完整的代码请私聊博主进行下载!!!
如有不懂可以私聊博主进行下载,博主可是一个热心肠哦!
一、实验目的及任务
通过该实验,掌握Spring面向切面编程常用方法,掌握利用Spring xml配置方式实现面向切面编程AOP。
二、实验环境及条件
主机操作系统为Win10,Tomcat,j2sdk1.6或以上版本。
三、实验实施步骤
略
四、实验报告内容
代码详见附件1
1 程序在控制台的输出结果是什么?给出截图。
图1:没有进行注解配置的时候的效果。
图2:采用注解配置的时候的效果。

2 在本程序中,对应概念切面, 切入点,前置通知(增强)和后置通知(增强)的配置和代码有哪些?切面类包含了哪些内容,切点表达式的作用是什么?
切面就是定义的Logger等类,对应的配置就是xml中定义aop:aspect
前置:beforeMethod,对应的xml的配置文件中有before的配置
后置:afterMethodm,对应的xml的配置文件中有after的配置
切面类:就是Loggerxxx
切面表达式:就是@Pointcut或者是aop:pointcut配置的内容,指定一种规则。
3 当调用add方法时,哪些切面方法被执行了?这些方法属于哪种类型的通知(增强)?
符合切面表达式子的都会执行。
4 在本程序中,如果用注解方式对除法(div)方法添加验证切面时(如,运行div除法之前先检查除数是否为0),应对VlidationAspect类做何种修改?
可以采用context扫描需要添加spring对aop的自动代理。
可以采用非context的扫描的方式,需要bean指定切面类的bean。
附件1
1.项目目录
2.xml配置文件代码
一个采用的是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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:component-scan base-package="xxx.xxx.aop"/>
<aop:aspectj-autoproxy/>
<!--配置bean -->
<bean id="arithmeticCalculator"
class="xxx.xxx.aop.xml.ArithmeticCalculatorImpl"></bean>
<!-- 配置切面的bean.-->
<bean id="loggingAspect"
class="xxx.xxx.aop.xml.LoggingAspect"></bean>
<bean id="vlidationAspect"
class="xxx.xxx.aop.xml.VlidationAspect">
</bean>
<!--配直AOP -->
<aop:config>
<!--配置切点表达式-->
<aop:pointcut id="pointcut" expression="execution(* xxx.xxx.aop.xml.ArithmeticCalculator.*(int , int ))"/>
<!-- 配置切面及通知-->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:around method="aroundMethod" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>
</beans>
3.pom依赖