Spring-常用Jar包
[http://pan.baidu.com/s/1dD1t92D]
aspect实例
所需jar包如上
bean配置文件aspect.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
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">
<!-- 配置自动扫描 -->
<context:component-scan base-package="com.bean.aopiml.aspect"></context:component-scan>
<!-- 使aspjectj 起作用 :自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
日志管理-切面类
package com.bean.aopiml.aspect;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
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;
/*
* 把这个类声明为一个切面:首先要放入到ioc容器中,然后在声明切面
* */
@Aspect
@Component
public class LoggingAspect {
@Before("execution(public * com.bean.aopiml.aspect.ArithmeticInter.*(int, int))")
public void beforeMethod(JoinPoint joinPoint){
String methodName =joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("Method - "+methodName+" begins with"+args);
}
/*
* 在目标方法执行后(无论是否发生异常) 后置通知
* 不能访问目标方法执行结果
* */
@After("execution(* com.bean.aopiml.aspect.ArithmeticInter.*(..))")
public void afterMethod(JoinPoint joinPoint){
String methodName =joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("Method - "+methodName+" end with"+args);
}
@AfterReturning(value="execution(* com.bean.aopiml.aspect.ArithmeticInter.*(..))",returning="result")
public void afterReturning(JoinPoint joinPoint,Object result){
String methodName =joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("Method - "+methodName+" result with :"+result);
}
@AfterThrowing(value="execution(* com.bean.aopiml.aspect.ArithmeticInter.*(..))" ,throwing="ex")
public void afterThrowing(JoinPoint joinPoint,Exception ex){
String methodName =joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("Method - "+methodName+" occurs exception :"+ ex);
}
/*
* 环绕通知需要携带 ProceedingJoinPoint 类型参数
* 环绕通知类似于动态代理的全过程 ProceedingJoinPoint 类型的参数可以决定是否执行目标方法
* 环绕通知 必须有返回值 且返回值就是方法的返回值
* */
@Around("execution(* com.bean.aopiml.aspect.ArithmeticInter.*(..))")
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){
String methodName =proceedingJoinPoint.getSignature().getName();
Object result=null;
List<Object> args=Arrays.asList(proceedingJoinPoint.getArgs());
try {
//前置通知
System.out.println("Method - "+methodName+" begins with"+args);
//执行目标方法
result=proceedingJoinPoint.proceed();
//返回通知
System.out.println("Method - "+methodName+" result with :"+result);
} catch (Throwable e) {
// TODO Auto-generated catch block
// 异常通知
System.out.println("Method - "+methodName+" occurs exception :"+ e);
e.printStackTrace();
}
//后置通知
System.out.println("Method - "+methodName+" end with"+args);
return result;
}
}
实例其他普通接口与类
- ArithmeticInter
package com.bean.aopiml.aspect;
public interface ArithmeticInter {
public int add(int i,int j);
public int sub(int i,int j);
public double mul(int i,int j);
public double div(int i,int j);
}
- Arithmetic
package com.bean.aopiml.aspect;
import org.springframework.stereotype.Component;
@Component
public class Arithmetic implements ArithmeticInter{
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 double mul(int i ,int j){
double result=i*j;
return result;
}
public double div(int i ,int j){
double result=i/(double)j;
return result;
}
}
- JunitTest
package com.bean.aopiml.aspect;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JunitTest {
ArithmeticInter arithmetic;
@Before
public void setUp() throws Exception {
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/bean/aopiml/aspect/beans-aspect.xml");
arithmetic =ctx.getBean(ArithmeticInter.class);
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
arithmetic.add(3, 6);
arithmetic.sub(3, 6);
arithmetic.mul(3, 6);
arithmetic.div(10, 0);
}
}