【spring】springAop开发

xml开发方式

 

springboot的使用

1、声明一个parent

代码实现:

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.0.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

2、去mvn官网找到找到对应的jar包,添加到依赖当中,将版本号删除(不需要版本号,在maven工程的对应的文件夹中有pom文件,其中就会标明版本号)

springAop开发所用的jar包

(1)spring-aop-4.3.3.RELEASE.jar

(2)aspectjweaver-1.8.5.jar

(3)aspectjrt-1.8.5.jar

开发步骤:

一、创建xml文件

1、复制表头

代码实现:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        
        
</beans>

2、注册业务bean

代码实现:

<bean id="bankService" class="com.zzxtit.spring.aop.xml.BankService"></bean>

3、注册aop的命名空间

代码实现:

xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd"

4、将文件添加到xfd文件夹中,并配置到eclipse当中(同之前博客)

https://blog.csdn.net/tyrant_forever/article/details/102919948

5、添加切面

代码实现:

	<aop:config>
		<aop:pointcut expression="execution(* springboot.aop.xml.*.*(..))" id="loggerPointCut"/>
		<aop:aspect ref="loggerAspect">
			<aop:before method="logerBefore" pointcut-ref="loggerPointCut"/>
		</aop:aspect>

	</aop:config>

注:

  (1)execution中的

第一个参数 : 返回值的类型

第二个参数 : 包名

第三个参数 : 类名

第四个参数 : 方法名

第五个参数 : 方法参数(..)代表所有的方法

(2)spring AOP拦截的是方法

(3)method指的是类中自定义的前置方法

(4)aop:before指的是前置通知,后置通知等待补充

创建通知类:

代码实现:

package com.zzxtit.spring.aop.xml;

import org.aspectj.lang.JoinPoint;

public class LoggerAspect {
	public void loggerBefore(JoinPoint jp) {
		String methodName = jp.getSignature().getName();
		System.out.println("method:" + methodName + "将要被执行!");
		
		Object[] args = jp.getArgs();
		
		for(Object arg : args) {
			System.out.println("=====>参数" + arg);
		}
	}
}

注解开发方式

简单实例

1、xml文件的配置

代码实现:

<context:component-scan base-package="com.zzxtit.spring.autoaop.anno"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

2、在业务bean上添加注解

3、在通知类里添加注解

代码实现:

@Aspect
@Component
public class LoggerAspect {

	@Before("execution(* com.zzxtit.spring.autoaop.anno.*.*(..))")
	public void logerBefore(JoinPoint jp) {

4、在main方法里运行

代码实现:

public class Main {
	public static void main(String[] args) {
		ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContest-anno-aop.xml");
		BankServiceImp bk = ioc.getBean("bankService", BankServiceImp.class);
		bk.transfer("唐浩然", "马云", new BigDecimal("100000000"));
	}
}

注:

1、aop是需要面向接口开发的所以必须使用接口实例化对象

重用切入点

代码实现:

	@Pointcut("execution(* com.zzxtit.spring.autoaop.anno.*.*(..))")
	public void loggerPointCut() {
		
	}
	
	@Before("loggerPointCut()")
	public void logerBefore(JoinPoint jp) {
		String methodName = jp.getSignature().getName();
		System.out.println("method: " + methodName + "将要执行!");
		
		Object[] args = jp.getArgs();
		for(Object arg : args) {
			System.out.println("=============参数:>" + arg);
		}
		
	}

注:

1、返回通知时,需用加入一个参数 returning

代码实现:

	@AfterReturning(value="loggerPointCut()", returning="returnValue")
	public void logerAfterReturning(JoinPoint jp, Object returnValue) {
		String methodName = jp.getSignature().getName();
		System.out.println("后置返回通知=========>method: " + methodName + "已经被执行!");
		System.out.println("后置返回通知 返回值:" + returnValue);
	}


2、如果在注解内没有注明参数直接写入值,则默认添加到value当中

3、返回通知注解中有一个参数pointcut当在其中添加值时,会覆盖value的值

发布了128 篇原创文章 · 获赞 37 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/tyrant_forever/article/details/103014925
今日推荐