spring学习随笔5

本文介绍aop的基于配置文件的方式

aopconfig.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:p="http://www.springframework.org/schema/p"
	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-4.3.xsd">
	
	<!-- 配置bean -->
	<bean id="calculator" class="com.liu.aopconfig.Calculator"></bean>
	<bean id="calculatorAspect" class="com.liu.aopconfig.CalculatorAspect"></bean>
	<bean id="validateAspect" class="com.liu.aopconfig.ValidateAspect"></bean>
	
	<!-- 配置切面 -->
	<aop:config>
		<aop:aspect id="aspect2" ref="calculatorAspect" order="2">
			<aop:pointcut expression="execution(public int com.liu.aopconfig.ICalculator.add(.. ))" id="pointcut1"/>
			<aop:pointcut expression="execution(public int com.liu.aopconfig.ICalculator.div(.. ))" id="pointcut2"/>
			<aop:pointcut expression="execution(public int com.liu.aopconfig.ICalculator.minus(.. ))" id="pointcut3"/>
			<aop:before method="beforeMethod" pointcut-ref="pointcut1"/>
			<aop:after method="afterMethod" pointcut-ref="pointcut1"/>
			<aop:after-returning method="afterReturn" pointcut-ref="pointcut1" returning="result"/>
			<aop:after-throwing method="afterThrow" pointcut-ref="pointcut2" throwing="e"/>
			<aop:around method="aroundMethod" pointcut-ref="pointcut3"/>
		</aop:aspect>
		<aop:aspect id="aspect1" ref="validateAspect" order="1">
			<aop:before method="beforeMethod" pointcut-ref="pointcut1"/>
		</aop:aspect>		
	</aop:config>
</beans>


ICalculator.java

package com.liu.aopconfig;

public interface ICalculator {
	public int add(int i,int j);
	public int minus(int i,int j);
	public int div(int i,int j);
}

Calculator.java

package com.liu.aopconfig;

import org.springframework.stereotype.Component;

public class Calculator implements ICalculator {

	@Override
	public int add(int i, int j) {
		return i + j;

	}

	@Override
	public int minus(int i, int j) {
		return i - j;
	}

	@Override
	public int div(int i, int j) {
		return i/j;
	}

}

CalculatorAspect.java

package com.liu.aopconfig;

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;

public class CalculatorAspect {


	public void declareAddMethod(){}
	
	public void beforeMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		System.out.println("The method "+ methodName+ " begin with"+args);
	}
	
	public void afterMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method "+ methodName+ " end ");
	}
	

	public void afterReturn(JoinPoint joinPoint,Object result){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method "+ methodName+ " return with " + result);		
	}
	
	public void afterThrow(JoinPoint joinPoint,Exception e){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method "+ methodName+ " occurs with " + e);		
	}
	

	public Object aroundMethod(ProceedingJoinPoint pjoinPoint){
		Object result = null;
		String methodName = pjoinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(pjoinPoint.getArgs());
		System.out.println("The method "+ methodName+ " begin with"+args);
		try {
			result = pjoinPoint.proceed();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("The method "+ methodName+ " return with "+result);
		return result;
	}
}


ValidateAspect.java

package com.liu.aopconfig;

import java.util.Arrays;
import java.util.List;

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;


public class ValidateAspect {

	public void beforeMethod(JoinPoint joinPoint){
		System.out.println("The method validate");
	}
}


Test.java

package com.liu.aopconfig;

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

public class Test {

	public static void main(String[] args) {
		ApplicationContext cxt = new ClassPathXmlApplicationContext("aopconfig.xml");
		
		ICalculator calculator = (ICalculator)cxt.getBean("calculator");
		calculator.add(1, 3);
		
		calculator.minus(1, 3);
		
		calculator.div(1, 0);
		


	}

}



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值