编写程序实现以下功能:
1.日志功能:在程序执行期间,追踪正在发生的活动(打印出调用的方法,以及参数的参数值)
2.验证功能:希望计算器只能处理正数的计算,当有负数参与运算时,给出提示说明
分析:首先采用OOP编程技术,然后采用AOP技术附加需求
OOP编程技术
- 编写业务逻辑接口
package com.edu.aop.xml;
public interface ArithmeticCalculator {
public int add(int i, int j);
public int sub(int i, int j);
public int mul(int i, int j);
public int div(int i, int j);
}
- 接口的实现类
package com.edu.aop.xml;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
- 添加bean配置信息
<bean id="abcd" class="com.edu.aop.xml.ArithmeticCalculatorImpl"></bean>
- 设计测试程序
package com.edu.aop.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main_xml {
public static void main(String[] args) {
//(1)创建IoC容器
String pathName="applicationContext_xml.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(pathName);
//(2)从容器IoC中获取对象(在类中已经采用注释)
ArithmeticCalculator ac = (ArithmeticCalculator) ctx.getBean("abcd");
//(3)使用对象
int result = ac.add(10, 20);
System.out.println("result:" + result);
result = ac.div(-21, 3);
System.out.println("result:" + result);
((ClassPathXmlApplicationContext)ctx).close();
}
}
AOP编程技术
设计 “切面” 要完成功能的 “切面类”
package com.edu.aop.xml;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
Object [] args = joinPoint.getArgs();
System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends");
}
}
修改配置文件,添加 AOP 如下的配置信息:
<!-- 配置切面类的一个bean -->
<bean id="logging" class="com.edu.aop.xml.LoggingAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切面及通知 -->
<aop:aspect ref="logging" >
<aop:before method="beforeMethod"
pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
<aop:after method="afterMethod"
pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
</aop:aspect>
</aop:config>
设置一个切面类,判定实参是否有负数:
package com.edu.aop.xml;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
public class VlidationAspect {
public void validateArgs(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
//取得参数
Object [] args = joinPoint.getArgs();
System.out.println("-->validate:" + Arrays.asList(args));
if (args != null && args.length > 0) {
for(int i=0;i<args.length;++i){
if(((Integer)args[i]).intValue()<0){
System.out.println("警告:方法"+methodName+"()第"+i+"参数为负数:" +args[i]);
}
}
}
}
}
再次修改配置文件,添加新的切面类的配置:
<?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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="abcd" class="com.edu.aop.xml.ArithmeticCalculatorImpl"></bean>
<!-- 配置切面类的一个bean -->
<bean id="logging" class="com.edu.aop.xml.LoggingAspect"></bean>
<bean id="xyz" class="com.edu.aop.xml.VlidationAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切面及通知 -->
<aop:aspect ref="logging" order="2">
<aop:before method="beforeMethod"
pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
<aop:after method="afterMethod"
pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
</aop:aspect>
<!-- 配置第2个切面及通知 -->
<aop:aspect ref="xyz" order="1">
<aop:before method="validateArgs"
pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
</aop:aspect>
</aop:config>
</beans>