Spring4学习笔记-AOP(基于注解的方式)

1.加入jar包

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

commons-logging-1.1.3.jar

spring-aop-4.1.0.RELEASE.jar

spring-aspects-4.1.0.RELEASE.jar

spring-beans-4.1.0.RELEASE.jar

spring-context-4.1.0.RELEASE.jar

spring-core-4.1.0.RELEASE.jar

spring-expression-4.1.0.RELEASE.jar

2.在配置文件中加入AOP的命名空间

3.基于注解的方式

①在配置文件中加入如下配置

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

②把横切关注点的代码抽象到切面的类中

切面首先是一个IOC中的bean,即加入@Component注解

切面还需要加入@Aspect注解

③在类中声明各种通知

@Before 前置通知,在方法执行之前执行

@After 后置通知,在方法执行之后执行

@AfterRunning 返回通知,在方法返回结果之后执行

@AfterThrowing 异常通知,在方法抛出异常之后执行

@Around 环绕通知,围绕着方法执行

③在方法中声明一个类型为JoinPoint的参数就可以访问链接细节

ArithmeticCalculator接口

package com.spring.aop.impl;

public interface ArithmeticCalculator {
	public int add(int i, int j);

	public int sub(int i, int j);

	public int mul(int i, int j);

	public int div(int i, int j);
}

接口实现类 ArithmeticCalculatorImpl.java

package com.spring.aop.impl;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator{

	@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;
	}

}

切面类 LoggingAspect.java

package com.spring.aop.impl;

import java.util.ArrayList;
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.core.annotation.Order;
import org.springframework.stereotype.Component;

//指定切面的优先级,当有多个切面时,数值越小优先级越高
@Order(1)
//把这个类声明为一个切面:需要把该类放入到IOC容器中。再声明为一个切面.
@Aspect
@Component
public class LoggingAspect {

	/**
	 * 声明切入点表达式,一般在该方法中不再添加其他代码。
	 * 使用@Pointcut来声明切入点表达式。
	 * 后面的通知直接使用方法名来引用当前的切入点表达式。
	 */
	@Pointcut("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(..))")
	public void declareJoinPointExpression() {}

	/**
	*前置通知,在目标方法开始之前执行。
	*@Before("execution(public int com.spring.aop.impl.ArithmeticCalculator.add(int, int))")这样写可以指定特定的方法。
	 * @param joinpoint
	 */
	@Before("declareJoinPointExpression()")
	//这里使用切入点表达式即可。后面的可以都改成切入点表达式。如果这个切入点表达式在别的包中,在前面加上包名和类名即可。
	public void beforeMethod(JoinPoint joinpoint) {
		String methodName = joinpoint.getSignature().getName();
		List<Object>args = Arrays.asList(joinpoint.getArgs());
		System.out.println("前置通知:The method "+ methodName +" begins with " + args);
	}

	/**
	*后置通知,在目标方法执行之后开始执行,无论目标方法是否抛出异常。
	*在后置通知中不能访问目标方法执行的结果。
	 * @param joinpoint
	 */
	@After("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(int, int))")
	public void afterMethod(JoinPoint joinpoint) {
		String methodName = joinpoint.getSignature().getName();
		//List<Object>args = Arrays.asList(joinpoint.getArgs());  后置通知方法中可以获取到参数
		System.out.println("后置通知:The method "+ methodName +" ends ");
	}

	/**
	*返回通知,在方法正常结束之后执行。
	*可以访问到方法的返回值。
	 * @param joinpoint
	 * @param result 目标方法的返回值
	 */
	@AfterReturning(value="execution(public int com.spring.aop.impl.ArithmeticCalculator.*(..))", returning="result")
	public void afterReturnning(JoinPoint joinpoint, Object result) {
		String methodName = joinpoint.getSignature().getName();
		System.out.println("返回通知:The method "+ methodName +" ends with " + result);
	}

	/**
	*异常通知。目标方法出现异常的时候执行,可以访问到异常对象,可以指定在出现特定异常时才执行。
	*假如把参数写成NullPointerException则只在出现空指针异常的时候执行。
	 * @param joinpoint
	 * @param e
	 */
	@AfterThrowing(value="execution(public int com.spring.aop.impl.ArithmeticCalculator.*(..))", throwing="e")
	public void afterThrowing(JoinPoint joinpoint, Exception e) {
		String methodName = joinpoint.getSignature().getName();
		System.out.println("异常通知:The method "+ methodName +" occurs exception " + e);
	}

	/**
	 * 环绕通知类似于动态代理的全过程,ProceedingJoinPoint类型的参数可以决定是否执行目标方法。
	 * @param point 环绕通知需要携带ProceedingJoinPoint类型的参数。
	 * @return 目标方法的返回值。必须有返回值。
	 */
	 /*不常用
	@Around("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(..))")
	public Object aroundMethod(ProceedingJoinPoint point) {
		Object result = null;
		String methodName = point.getSignature().getName();
		try {
			//前置通知
			System.out.println("The method "+ methodName +" begins with " + Arrays.asList(point.getArgs()));
			//执行目标方法
			result = point.proceed();
			//翻译通知
			System.out.println("The method "+ methodName +" ends with " + result);
		} catch (Throwable e) {
			//异常通知
			System.out.println("The method "+ methodName +" occurs exception " + e);
			throw new RuntimeException(e);
		}
		//后置通知
		System.out.println("The method "+ methodName +" ends");
		return result;
	}
	*/
}

applicationContext.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: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-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
	<!-- 配置自动扫描包 -->
	<context:component-scan base-package="com.spring.aop.impl"></context:component-scan>
	<!-- 使AspectJ注解起作用:自动为匹配的类生产代理对象 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

Main.java

package com.spring.aop.impl;

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

public class Main {
public static void main(String[] args) {
	//创建spring IOC容器
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	//从IOC容器中获取bean实例
	ArithmeticCalculator arithmeticCalculator = applicationContext.getBean(ArithmeticCalculator.class);
	int result = arithmeticCalculator.add(4, 6);
	System.out.println(result);
	result = arithmeticCalculator.sub(4, 6);
	System.out.println(result);
	System.out.println(result);
	result = arithmeticCalculator.mul(4, 6);
	System.out.println(result);
	System.out.println(result);
	result = arithmeticCalculator.div(4, 0);
	System.out.println(result);
}
}

源码

http://yunpan.cn/cgsrQHmUvrIQt  提取码 6564

时间: 2024-08-27 19:41:42

Spring4学习笔记-AOP(基于注解的方式)的相关文章

Spring4学习笔记-AOP(基于配置文件的方式)

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引入的jar包与基于注解的方式引入的jar包相同 ArithmeticCalculator接口 1 2 3 4 5 6 7 8 9 10 11 package com.spring.aop.impl.xml; public interface ArithmeticCalculator {     pu

Spring4学习笔记-AOP

1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar commons-logging-1.1.3.jar spring-aop-4.1.0.RELEASE.jar spring-aspects-4.1.0.RELEASE.jar spring-beans-4.1.0.RELEASE.jar spring-context-4.1.0.RELE

Spring学习笔记-springMVC基于注解的控制器(Demo)

springmvc的整体运行流程图: 基于@Controller和@RequestMapping是springmvc示例代码 在web.xml中配置springmvc核心分发器DispatcherServlet .... <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </

Spring学习笔记-springMVC基于注解的控制器(基本概念)

在spring2.5以前的版本中,实现一个mvc的Controller的唯一方法就是实现Controller接口,一个控制器只能响应一个客户端请求,在2.5以后的版本中,spring引入了注解,利用注解简化配置文件,利用注解实现bean的声明和依赖注入(DI),注解也同样被引入到spring的web模块springMVC中. 使用基于注解的控制器有两个优点 第一:一个控制器可以处理多个动作,而不是像以前那样一个控制器只能处理一个请求 第二:省略的在配置文件中对bean的声明和依赖注入,显著提高开

Spring AOP基于注解的“零配置”方式

Spring AOP基于注解的“零配置”方式: Spring的beans.xml中 <!-- 指定自动搜索Bean组件.自动搜索切面类 --> <context:component-scan base-package="org.crazyit.app.service,org.crazyit.app.aspect"> <context:include-filter type="annotation" expression="or

spring学习5:基于注解实现spring的aop

目录 spring学习5:基于注解实现spring的aop 一.基于注解+xml实现 1.1 在配置文件中开启spring对注解aop的支持 1.2 把通知类用注解配置到容器中,并用注解声明为切面 1.3 定义切入点表达式 1.4 定义通知 二.基于纯注解实现 三.多个aop的执行顺序 1.xml配置 2.注解配置 3.注意 spring学习5:基于注解实现spring的aop 上一节学习了spring aop的基本概念和如何基于xml配置来实现aop功能.这一节来学习下如何用注解实现aop 一

springmvc学习笔记(11)-springmvc注解开发之简单参数绑定

springmvc学习笔记(11)-springmvc注解开发之简单参数绑定 springmvc学习笔记11-springmvc注解开发之简单参数绑定 spring参数绑定过程 默认支持的类型 简单类型 pojo绑定 自定义参数绑定实现日期类型绑定 springmvc和struts2的区别 本文主要介绍注解开发的简单参数绑定,包括简单类型.简单pojo以及自定义绑定实现类型转换 spring参数绑定过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到contro

嵌入式Linux学习笔记(基于S5PV210 TQ210)

基于S5PV210.TQ210平台. 本文更多的是教会大家如何学习! 包括如下内容: 1.前言 2.开发环境搭建 3.制作交叉编译器 4.裸机编程 4.1.汇编学习 4.2.S5PV210启动流程 4.3.点亮一个LED 4.4.串口 4.5.实现printf 4.6.时钟配置 4.7.重定位 4.8.DDR 4.9.NAND读写 4.11.LCD操作 5.移植u-boot(基于u-boot-2014.4版本) 5.1.概述 5.2.u-boot配置过程分析 5.3.u-boot编译过程分析 5

.NET Remoting学习笔记(二)激活方式

目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 参考:百度百科  ♂风车车.Net 激活方式概念 在访问远程类型的一个对象实例之前,必须通过一个名为Activation的进程创建它并进行初始化.这种客户端通过通道来创建远程对象,称为对象的激活. 激活分为两大类:服务器端激活  客户端激活 服务器端激活 又称WellKnow(知名对象) 服务器应用程序在激活对象实例之前会在一个众所周知的统一资源标识符(URI)上来发布这个类型.然后该服务器进程