81,092
社区成员
发帖
与我相关
我的任务
分享
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 dev(int i,int j);
}
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
public int add(int i, int j) {
int result =i+j;
return result;
}
public int sub(int i, int j) {
int result =i-j;
return result;
}
public int mul(int i, int j) {
int result =i*j;
return result;
}
public int dev(int i, int j) {
int result =i/j;
return result;
}
}
@Component(value="loggingAspect")
public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint){
/*String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());*/
System.out.println("the method "/*+methodName+" begins with "+args*/ );
}
public void afterMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("the method "+methodName+" ends" );
}
public void afterReturnMethod(JoinPoint joinPoint,Object result){
String methodName=joinPoint.getSignature().getName();
System.out.println("the method "+methodName+" end ,and result with "+result );
}
public void afterThrowing(JoinPoint joinPoint,Exception ex){
String methodName=joinPoint.getSignature().getName();
System.out.println("the method "+methodName+" end ,and Excetion with "+ex );
}
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result=null;
String methodName=pjd.getSignature().getName();
try {
//前置通知
System.out.println("the method "+methodName+" begins with " +Arrays.asList(pjd.getArgs()));
//执行目标方法
result =pjd.proceed();
//返回通知
System.out.println("the method "+methodName+" return with " + result);
} catch (Throwable e) {
// 异常通知
e.printStackTrace();
}
//后置通知
System.out.println("the method "+methodName+" ends");
return result;
}
}
@SuppressWarnings("resource")
public class XmlTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextXml.xml");
ArithmeticCalculatorImpl manager = context.getBean(ArithmeticCalculatorImpl.class);
int x = manager.add(3, 4);
System.out.println(x);
}
}
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy/>
<!-- 指定自动搜索Bean组件,自动搜索切面类 -->
<context:annotation-config/>
<context:component-scan base-package="com.xml">
</context:component-scan>
<!-- Vincent's xml property -->
<!-- 配置aop 切入点,切面,通知-->
<aop:config>
<!-- 配置拦截表达式 切入点:符合切入点表达式的类要产生代理对象,expression就是切入点表达式 -->
<aop:pointcut id="testOperation"
expression="execution(* com.xml.arithmeticcalculator.ArithmeticCalculator*.*(..))"/>
<!-- 配置切面 -->
<aop:aspect id="calculatorloggingAspect" ref="LoggingAspect">
<!--前置通知
1、在目标方法执行之前
2、前置通知中,方法有一个参数JoinPoint -->
<aop:before method="beforeMethod" pointcut-ref="testOperation" />
<!--后置通知
1、在目标方法执行之后
2、能够获取目标方法的返回值 result
returning="result" result就是切面中 后置通知中的参数名称 (须一致)
3、如果目标方法产生异常,则后置通知不再执行 -->
<aop:after-returning method="afterReturnMethod" pointcut-ref="testOperation" returning="result"/>
<!--异常通知
获取目标方法抛出的异常信息 throwing="ex" -->
<aop:after-throwing method="afterThrowing" pointcut-ref="testOperation" throwing="ex"/>
<!-- 最终通知 -->
<aop:after method="afterMethod" pointcut-ref="testOperation"/>
<!-- 环绕通知
能够控制目标方法的执行
环绕通知可以有返回值,这个返回值就是代理对象的方法的返回值
前置通知和后置通知只能在目标方法执行之前和之后加代码,但是不能控制目标方法的执行 -->
<!-- <aop:around method="aroundMethod" pointcut-ref="testOperation"/> -->
</aop:aspect>
</aop:config>
</beans>
//ArithmeticCalculatorImpl manager = context.getBean(ArithmeticCalculatorImpl.class);
ArithmeticCalculatorImpl manager = context.getBean("arithmeticCalculatorImpl");