spring的面向切面程式設計
spring中的切面的程式設計中的使用註冊的方式實現:
介面:
package com.wqq.aop;
public interface AtithmeticCaculator { public abstract int add(int i,int j); public abstract int sub(int i,int j); public abstract int mul(int i,int j); public abstract int div(int i,int j);
}
實現類
package com.wqq.aop;
import org.springframework.stereotype.Component;
@Component public class AtithmeticCaculatorIm implements AtithmeticCaculator{
@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; }
}
日誌類
package com.wqq.aop;
import java.util.Arrays; import java.util.List;
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.core.annotation.Order; import org.springframework.stereotype.Component;
//這兒類是一個切面:放在IOC的容器中,神明一個切面 @Order(1) @Aspect @Component public class LoggingAspect { //宣告@Pointcut 來宣告一個切入點的表示式 @Pointcut("execution(public int com.wqq.aop.AtithmeticCaculator.*(int ,int ))") public void declareJointPoinExpression(){ } @Before("declareJointPoinExpression()") public void beforeMethod(JoinPoint joinPoint){ //獲取引數的值 String methodName=joinPoint.getSignature().getName(); List<Object> args=Arrays.asList(joinPoint.getArgs()); System.out.println("---------Before--------"+args); } //後置通知 @After("declareJointPoinExpression()") public void afterMethod(JoinPoint joinPoint){ //獲取引數的值 String methodName=joinPoint.getSignature().getName(); List<Object> args=Arrays.asList(joinPoint.getArgs()); System.out.println("----after -----------------"+args); } //返回通知。可以返回到方法的返回值 @AfterReturning(value="execution(public int com.wqq.aop.AtithmeticCaculator.*(int ,int ))",returning="result") public void afterReturningMethod(JoinPoint joinPoint,Object result){ System.out.println("返回通知的---"+result); } }
測試類
package com.wqq.aop;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test { public static void main(String[] args) { ApplicationContext context=new FileSystemXmlApplicationContext("application.xml"); //context.getBean("student"); AtithmeticCaculator a=(AtithmeticCaculator)context.getBean("atithmeticCaculatorIm"); System.out.println(a.add(1, 1)); } }
檔案配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:drools="http://drools.org/schema/drools-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd">
<!-- 掃描包中的類 --> <context:component-scan base-package="com.wqq.aop"></context:component-scan> <aop:aspectj-autoproxy /> <!-- 將資料庫的bean載入進來 -->
</beans>