spring-AOP(基于配置文件方式实现)

不再使用注解,所有的类都是普通类

切面类:

package com.cwh.spring.aop.xml;

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

import javax.naming.spi.DirStateFactory.Result;

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;

//[@Component](https://my.oschina.net/u/3907912)
public class LoginAspect {


	public void declareJoinpointExprisseion(){}
	

	public void beforMethod(JoinPoint joinPoint){
		//切入点的方法名
		String methodName = joinPoint.getSignature().getName();
		//切入点方法入参
		List<Object> list = Arrays.asList(joinPoint.getArgs());
		System.out.println("the method:"+methodName+" begin with:"+list);
	}

	public void afterMethod(JoinPoint joinPoint){
		//切入点的方法名
		String methodName = joinPoint.getSignature().getName();
//		//切入点方法入参
//		List<Object> list = Arrays.asList(joinPoint.getArgs());
		System.out.println("the method:"+methodName+" end");
	}
	

	public void afterReturnMethod(JoinPoint joinPoint, Object result){
		System.out.println("r方法:" + joinPoint.getSignature().getName() + " 结果:" + result);
	}
	

	public void afterThrowing(JoinPoint joinPoint, Exception exception){
		System.out.println("t方法:" + joinPoint.getSignature().getName() + " 发生错误:" + exception);
	} 
	

	public Object around(ProceedingJoinPoint joinPoint){
		Object result = null;
		String methodName = joinPoint.getSignature().getName();
		
		try {
			//前置通知
			System.out.println("方法前置通知:" + methodName + "参数:" + Arrays.asList(joinPoint.getArgs()));
			//执行目标方法
			result = joinPoint.proceed();
			//后置通知
			System.out.println("方法返回通知:" + result);
		} catch (Throwable e) {
//			e.printStackTrace();
			//异常通知
			System.out.println("the method "+ methodName +" occurs exception:"+e);
		}
		//后置通知
		System.out.println("the method "+ methodName +" ends");
		return result;
	}
}

切面类2:

package com.cwh.spring.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

/**
 * 切面类2
 */
public class VlidationAspect {

	private void vlidateArgs(JoinPoint joinpoint) {
		System.out.println("validateArgs:" + Arrays.asList(joinpoint.getArgs()));
	}
}

接口:

package com.cwh.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);
	
}

实现类:

package com.cwh.spring.aop.xml;

import org.springframework.stereotype.Component;

//@Component(value = "arithmeticCalculatorImpl")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	[@Override](https://my.oschina.net/u/1162528)
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}

	[@Override](https://my.oschina.net/u/1162528)
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}

	[@Override](https://my.oschina.net/u/1162528)
	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;
	}

}

配置文件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"
	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.0.xsd">

	<!-- 普通bean -->
	<bean id="arithmeticCalculatorImpl"
		class="com.cwh.spring.aop.xml.ArithmeticCalculatorImpl">
	</bean>

	<!-- aop的bean -->
	<bean id="loginAspect" class="com.cwh.spring.aop.xml.LoginAspect"></bean>
	<bean id="vlidationAspect" class="com.cwh.spring.aop.xml.VlidationAspect"></bean>
	
	<!-- 配置aop -->
	<aop:config>
		<!-- 切点配置 -->
		<aop:pointcut expression="execution(* com.cwh.spring.aop.xml.ArithmeticCalculator.*(..))" 
			id="pointCut"/>
		<!-- 切面及通知配置 -->
		<aop:aspect ref="loginAspect" order="2">
			<!-- 相当于后面的四个切面配置 
			 <aop:around method="around" pointcut-ref="pointCut"/>
			 -->
			 <!-- 可以单独写pointCut,也可以引用上面配置的 -->
			<aop:before method="beforMethod" pointcut="execution(* com.cwh.spring.aop.xml.ArithmeticCalculator.*(..))"/>
			<aop:after method="afterMethod" pointcut-ref="pointCut"/>
			<aop:after-returning method="afterReturnMethod" pointcut-ref="pointCut" returning="result"/>
			<aop:after-throwing method="afterThrowing" pointcut-ref="pointCut" throwing="exception"/>
		</aop:aspect>
		<aop:aspect ref="vlidationAspect" order="1">
			<aop:before method="toString" pointcut-ref="pointCut"/>
		</aop:aspect>
	</aop:config>
</beans>

测试类:

package com.cwh.spring.aop.xml;

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

public class Main {

	public static void main(String[] args) {
		//容器
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
		//获取bean
		ArithmeticCalculator ari = (ArithmeticCalculator) context.getBean("arithmeticCalculatorImpl");
		
		//使用bean
		int result = ari.add(5, 3);
		System.out.println("main结果:"+result);	
		
		int result2 = ari.div(15, 3);
		System.out.println("main结果:"+result2);	
	}
}

输出结果:

转载于:https://my.oschina.net/u/3780366/blog/1832689

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值