Spring之AOP基于注解配置切面
①新建接口
public interface Cacl { int add(int i,int j); int sub(int i,int j); int mul(int i,int j); int div(int i,int j); }
②实现接口
import org.springframework.stereotype.Component; @Component public class CaclImpl implements Cacl { @Override public int add(int i, int j) { // TODO Auto-generated method stub int result = i+j; return result; } @Override public int sub(int i, int j) { // TODO Auto-generated method stub int result = i-j; return result; } @Override public int mul(int i, int j) { // TODO Auto-generated method stub int result = i*j; return result; } @Override public int div(int i, int j) { // TODO Auto-generated method stub int result = i/j; return result; } }
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.springframework.stereotype.Component; @Aspect @Component public class Log { @Before("execution(* com.atguigu.spring.aop.impl.Cacl.*(int,int))") public void before(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("before method"+methodName+" begins with "+args); } //后置通知,无论是否出现异常 @After("execution(* com.atguigu.spring.aop.impl.Cacl.*(int,int))") public void after(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("after method "+methodName+" ends"); } @AfterReturning(value="execution(* com.atguigu.spring.aop.impl.Cacl.*(int,int))",returning="result") public void afterReturning(JoinPoint joinPoint,Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("afterreturning method "+methodName+" ends with "+result); } @AfterThrowing(value="execution(* com.atguigu.spring.aop.impl.Cacl.*(int,int))",throwing="exception") public void afterThrowing(JoinPoint joinPoint,Exception exception){ String methodName = joinPoint.getSignature().getName(); System.out.println("afterthrowing method "+methodName +"occurs by "+exception); } @Around("execution(* com.atguigu.spring.aop.impl.Cacl.*(int,int))") public Object around(ProceedingJoinPoint pjp){ Object result = null; String methodName = pjp.getSignature().getName(); try { System.out.println(" around method "+methodName +" begins with "+Arrays.asList(pjp.getArgs())); result = pjp.proceed(); System.out.println("around method "+methodName +" ends "); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
这些东西都不难,基本都是固定流程
③
<?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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.atguigu.spring.aop.impl"></context:component-scan> <!-- 使Aspectj注解起作用:自动为匹配的类生成代理对象 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
④
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); Cacl cacl = ctx.getBean(Cacl.class); System.out.println(cacl.add(2,3)); System.out.println(cacl.div(10,1)); } }
⑤输出结果
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. before methodadd begins with [2, 3] around method add begins with [2, 3] after method add ends around method add ends afterreturning method add ends with 5 5 before methoddiv begins with [10, 1] around method div begins with [10, 1] after method div ends around method div ends afterreturning method div ends with 10 10