1. 程式人生 > >Spring框架(三)AOP

Spring框架(三)AOP

 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>

 代理類

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/>