欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring框架(三)AOP

程序员文章站 2022-05-06 21:15:29
...

 AOP为了解决动态代理的繁琐,而产生的一种方便实现动态代理的简单框架

动态代理简单的来说就是将经常要用,重复的代码放到一个代理类里,在其他类里面调用就行,不用再每次重复写。

1.将要下载的包放到pom.xml里面

<?xml version="1.0"?>
<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>edu.edu.edu</groupId>
	<artifactId>es</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/log4j/log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>

	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2.编写类路径下的配置文件

<?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:context="http://www.springframework.org/schema/context"
    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/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        
        ">
 <!-- spring工厂扫描Bean的配置 配置扫描的包及子包下所有的类,被扫描到的类如果包含组件注解,就会创建这个类的实例 -->

<context:component-scan base-package="esl"></context:component-scan>

</beans>

方法一:AOP手动代理

手动代理:代理类里面没有注释

在配置文件里编写aop的配置

<!-- 进行aop的配置 -->
	<aop:config>
		<!-- 配置切入点表达式:哪些类的哪些方法需要进行增强 -->
		<aop:pointcut expression="execution(* esl..*.*(..))" id="pointcut1"/>
		
		<!-- 配置切面 -->
		<aop:aspect ref="my">
			<aop:before method="before" pointcut-ref="pointcut1"/>
		 
			<aop:after method="before1" pointcut-ref="pointcut1"/>
			 <aop:after-returning method="before2" pointcut-ref="pointcut1"/>
			
			<aop:after-throwing method="before3" pointcut-ref="pointcut1" />
			<aop:around method="before4" pointcut-ref="pointcut1"/>
	
		</aop:aspect>
	</aop:config>

Spring框架(三)AOP

 代理类

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

//代理类
@Component("my")
public class My {
	// 前置增强
	public void before() {
		System.out.println("前置增强===========");
	}

	public void before1() {
		System.out.println("结束执行");
	}

	public void before2() {
		System.out.println("结束并返回执行");
	}

	public void before3() {
		System.out.println("抛出异常");
	}

	public void before4(ProceedingJoinPoint p) throws Throwable {
		System.out.println("前前前");
		p.proceed();
		System.out.println("吼吼吼");
	}
}

方法二:AOP自动代理

自动代理:代理类里面有注释

代理类(注意:一定要加@Aspest  面向切面编程)

@Component
@Aspect   //******
public class MyAspectJ {
	@Before("aaa()")
	public void advise(){
		System.out.println("开始执行");
	}
	@After("MyAspectJ.aaa()")
	public void advise1(){
		System.out.println("结束执行");
	}
	@AfterReturning("aaa()")
	public void advise2(){
		System.out.println("结束并返回执行");
	}
	
	public void advise3(){
		System.out.println("抛出异常!");
	}
	@Around("execution(* *.*.*(..))")
	public void advise4(ProceedingJoinPoint p) throws Throwable{
		System.out.println("sdfadfadsf");
		p.proceed();
		System.out.println("45345345345");
	}
	
	@Pointcut("execution(* *.*.*(..))")
	private void aaa(){}
}

配置文件里配置动态代理

<!-- 开启aop注解的自动代理 -->
 <aop:aspectj-autoproxy/>

Spring框架(三)AOP