您的位置:首页 > 编程语言 > Java开发

Spring基于xml配置bean AOP

2017-01-04 00:00 337 查看
摘要: Spring基于xml配置bean AOP

1.。ArithmeticCalculator.java

package com.huangliusong.spring.aop.xml;

public interface ArithmeticCalculator {
int add(int i, int j);

int sub(int i, int j);

int mul(int i, int j);

int div(int i, int j);
}


2。ArithmeticCalculatorImpl.java

package com.huangliusong.spring.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;
}

}


3。LoggingAspect.java

package com.huangliusong.spring.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("方法开始——————" + methodName + Arrays.asList(args));
}

public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("方法结束——————" + methodName + Arrays.asList(args));
}

public void afterRunning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("方法结束——————" + methodName + Arrays.asList(args)
+ result);
}

public void afterThrowing(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out
.println("方法结束——————" + methodName + Arrays.asList(args) + ex);
}

public Object aroundMethod(ProceedingJoinPoint point) {
Object result = null;
String methodName = point.getSignature().getName();
// 执行目标方法
try {
// 前置通知
System.out.println("aroundMethod-" + methodName + "-begin with "
+ Arrays.asList(point.getArgs()));
result = point.proceed();
// 后置通知
System.out.println("aroundMethod-" + methodName + " -end with "
+ Arrays.asList(point.getArgs()));

} catch (Throwable e) {
// 异常通知
System.err.println(e);
e.printStackTrace();
}
return result;
}
}


4。TestAop.java

package com.huangliusong.spring.aop.xml;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {
@Test
public void test1() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext_xml.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx
.getBean("arithmeticCalculator");
System.err.println(arithmeticCalculator.getClass().getName());
int result = arithmeticCalculator.add(11, 11);
System.out.println("结果" + result);
result = arithmeticCalculator.div(12, 10);
System.out.println("结果" + result);
}
}


5。VlitationAspect.java

package com.huangliusong.spring.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 验证通知 可以指定切面的优先级
* 优先级使用Order(1)值越小优先级越高
* @author Administrator
*
*/
public class VlitationAspect {
public void validateArgs(JoinPoint joinPoint){
System.err.println("validateArgs"+Arrays.asList(joinPoint.getArgs()));
}
}

applicationContext_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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 配置bean -->
<bean id="arithmeticCalculator" class="com.huangliusong.spring.aop.xml.ArithmeticCalculatorImpl"></bean>

<!-- 配置切面的bean -->
<bean id="loggingAspect" class="com.huangliusong.spring.aop.xml.LoggingAspect"></bean>

<bean id="vlidationAspect" class="com.huangliusong.spring.aop.xml.VlitationAspect"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(public  * com.huangliusong.spring.aop.xml.*.*(..))" id="pointcut"/>
<!-- 配置切面以及通知 -->
<aop:aspect ref="loggingAspect"  order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
</aop:aspect>
<aop:aspect ref="vlidationAspect" order="1">
<aop:before method="validateArgs" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>


运行:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring