Spring AOP使用示例

Spring AOP使用示例AOP:【面向切面编程】指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。1、导入aop模块:SpringAOP:(spring-aspects)2、定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候讲日志进行打印(方法之前、方法运行结束、方法出现异常等)3、定义一个日志切面类(LOgAspects);切面类里面…

AOP:【面向切面编程】

         指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。
 
 1、导入aop模块:Spring AOP:(spring-aspects)
 2、定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候讲日志进行打印(方法之前、方法运行结束、方法出现异常等)
 3、定义一个日志切面类(LOgAspects);切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行对应的切面方法;
         通知方法:
                 前置通知(@Before):logStart:在目标方法div()运行之前运行
                 后置通知(@After):logEnd:在目标方法div()运行结束之后运行
                 返回通知(@AfterReturning):logReturn:在目标方法div()正常返回之后运行
                 异常通知(@AfterThrowing):logException:在目标方法div()出现异常之后运行
                 环绕通知:动态代理,手动推进目标方法运行(joinPoint.procced())
 4、给切面类的目标方法标注何时何地运行(通知注解)
 5、将切面类和业务逻辑类(目标方法所在类)都加入到容器中;
 6、必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect)
 7※给配置类中加@EnableAspectJAutoProxy 开启基于注解的AOP模式
         在Spring中很多的@EnableXXX都是表示要开启XXX功能

 主要三步:
 1、将业务逻辑组件和切面类都加入到容器中;告诉Spring哪个类是切面类(@Aspect)
 2、在切面类上的每一个通知方法上标注通知注解,告诉Spring何时何地运行(切入点表达式)
 3、开启基于注解的AOP模式;@EnableAspectJAutoProxy

代码示例:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.spring</groupId>
	<artifactId>spring-annotation</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.1.5.RELEASE</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>5.1.5.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
		<dependency>
		    <groupId>com.mchange</groupId>
		    <artifactId>c3p0</artifactId>
		    <version>0.9.5.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>5.1.40</version>
		</dependency>
				
	</dependencies>
</project>
package com.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.spring.aop.LogAspects;
import com.spring.aop.MathCalculator;


/**
 * AOP:【动态代理】
 * 		指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。
 * 
 * 1、导入aop模块:Spring AOP:(spring-aspects)
 * 2、定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候讲日志进行打印(方法之前、方法运行结束、方法出现异常等)
 * 3、定义一个日志切面类(LOgAspects);切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行对应的切面方法;
 * 		通知方法:
 * 				前置通知(@Before):logStart:在目标方法div()运行之前运行
 * 				后置通知(@After):logEnd:在目标方法div()运行结束之后运行
 * 				返回通知(@AfterReturning):logReturn:在目标方法div()正常返回之后运行
 * 				异常通知(@AfterThrowing):logException:在目标方法div()出现异常之后运行
 * 				环绕通知:动态代理,手动推进目标方法运行(joinPoint.procced())
 * 4、给切面类的目标方法标注何时何地运行(通知注解)
 * 5、将切面类和业务逻辑类(目标方法所在类)都加入到容器中;
 * 6、必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect)
 * 7※给配置类中加@EnableAspectJAutoProxy 开启基于注解的AOP模式
 * 		在Spring中很多的@EnableXXX都是表示要开启XXX功能
 *
 * 主要三步:
 * 1、将业务逻辑组件和切面类都加入到容器中;告诉Spring哪个类是切面类(@Aspect)
 * 2、在切面类上的每一个通知方法上标注通知注解,告诉Spring何时何地运行(切入点表达式)
 * 3、开启基于注解的AOP模式;@EnableAspectJAutoProxy
 *
 */

@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAop {
	
	//业务逻辑类加入到容器中
	@Bean
	public MathCalculator mathCalculator() {
		System.out.println("mathCalculator bean");
		return new MathCalculator();
	}

	//切面类加入到容器中
	@Bean
	public LogAspects logAspects() {
		return new LogAspects();
	}
}
package com.spring.aop;

public class MathCalculator {

	public int div(int i, int j) {
		System.out.println("MathCalculator >> div");
		return i / j;
	}

}
package com.spring.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LogAspects {
	
	//抽取公共的切入点表达式
	//1、本类引用
	//2、其他的切面引用
	@Pointcut("execution(public int com.spring.aop.MathCalculator.*(..))")
	private void pointCut(){};
	
	//@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入)
	//JoinPoint一定要出现在参数列表的第一位
	@Before(value = "pppointCut()")
	public void logStart(JoinPoint joinpoint) {
		System.out.println("logStart>>>>"+joinpoint.getSignature().getName()+">>>>"+Arrays.toString(joinpoint.getArgs()));
	}

	@After(value ="com.spring.aop.LogAspects.pppointCut()")
	public void logEnd(JoinPoint joinpoint) {
		System.out.println("logEnd>>>>>"+joinpoint.getSignature().getName()+">>>>"+Arrays.toString(joinpoint.getArgs()));
	}

	@AfterReturning(value ="execution(public int com.spring.aop.MathCalculator.*(..))",returning="object")
	public void logReturn(Object object) {
		System.out.println("logReturn>>>>"+object);
	}

	@AfterThrowing(value = "execution(public int com.spring.aop.MathCalculator.*(..))",throwing = "object")
	public void logException(Exception object) {
		System.out.println("logException>>>>"+object);
	}

}
package com.spring.test;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.spring.aop.MathCalculator;
import com.spring.config.MainConfigOfAop;

public class IOCTestAOP {
	@Test
	public void test01() {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAop.class);
		
		MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
		mathCalculator.div(10, 0);
		applicationContext.close();
	}

}

今天的文章Spring AOP使用示例分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/13183.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注