「では春のJDKのダイナミックプロキシ」と「春CGLlBのダイナミックプロキシ」、マニュアルは、次の2つの方法AOPプロキシによる以下の説明説明 春 春AOPプロキシを作成する方法を通知します。
春の通知タイプ
前回の調査から、通知(アドバイス)は実際にはターゲットエントリポイントのコンテンツであることがわかります。SpringAOPは、通知(アドバイス)にorg.aopalliance.aop.Adviceインターフェースを提供します。
Spring通知は、表1に示すように、ターゲットクラスメソッド内の接続ポイントの場所に応じて、次の5つのタイプに分類できます。
名前 | 解説 |
---|---|
org.springframework.aop.MethodBeforeAdvice(事前通知) | メソッドの前に自動的に実行される通知は事前通知と呼ばれ、権限管理などの機能に適用できます。 |
org.springframework.aop.AfterReturningAdvice(通知後) | メソッドの後に自動的に実行される通知はポスト通知と呼ばれ、ストリームのクローズ、ファイルのアップロード、一時ファイルの削除などの機能に適用できます。 |
org.aopalliance.intercept.MethodInterceptor(サラウンド通知) | メソッドの前後に自動的に実行される通知はサラウンド通知と呼ばれ、ロギングやトランザクション管理などの機能に適用できます。 |
org.springframework.aop.ThrowsAdvice(例外通知) | メソッドが例外をスローしたときに自動的に実行される通知は、例外通知と呼ばれ、例外記録ログの処理などの機能に適用できます。 |
org.springframework.aop.IntroductionInterceptor(紹介通知) | ターゲットクラスにいくつかの新しいメソッドと属性を追加します。これを適用して、プログラムの古いバージョンを変更できます(拡張クラス)。 |
宣言的な春のAOP
SpringのAOPプロキシを作成する基本的な方法は、org.springframework.aop.framework.ProxyFactoryBeanを使用することです。このクラスに対応するエントリポイントと通知は、完全な制御機能を提供し、指定されたコンテンツを生成できます。
ProxyFactoryBeanクラスの一般的に構成可能なプロパティを表2に示します。
属性名 | 描 述 |
---|---|
目標 | 対象読者 |
proxyInterfaces | エージェントによって実装されるインターフェース。複数のインターフェースがある場合は、次のフォーマットを使用して値を割り当てることができます: <list> <value> </ value> ... </ list> |
プロキシターゲットクラス | trueに設定した場合、インターフェースではなくクラスプロキシにCGLIBプロキシを使用するかどうか |
インターセプター名 | 移植する必要があるアドバイス |
シングルトン | 返されるエージェントがシングルトンかどうかにかかわらず、デフォルトはtrue(シングルトンを返す)です。 |
最適化する | trueに設定されている場合、CGLIBは必須です |
Spring通知では、サラウンド通知は非常に典型的なアプリケーションです。以下は、通知の場合を通してSpringでAOPプロキシを作成するプロセスを示しています。
1. JARパッケージをインポートする
コアJARパッケージに基づいて、次のように、AOP JARパッケージをspringDemo03プロジェクトのlibディレクトリにインポートします。
- spring-aop-3.2.13.RELEASE.jar:これは、Springパッケージですでに提供されている、AOPのSpringによって提供される実装です。
- com.springsource.org.aopalliance-1.0.0.jar:是 AOP 提供的规范,可以在 Spring 的官网网址 https://repo.spring.io/webapp/#/search/quick/ 中进行搜索并下载。
2. 创建切面类 MyAspect
在 src 目录下创建一个名为 com.mengma.factorybean 的包,在该包下创建切面类 MyAspect,如下所示。
package com.mengma.factorybean;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
//需要实现接口,确定哪个通知,及告诉Spring应该执行哪个方法
public class MyAspect implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {
System.out.println("方法执行之前");
// 执行目标方法
Object obj = mi.proceed();
System.out.println("方法执行之后");
return obj;
}
}
上述代码中,MyAspect 类实现了 MethodInterceptor 接口,并实现了接口的 invoke() 方法。MethodInterceptor 接口是 Spring AOP 的 JAR 包提供的,而 invoke() 方法用于确定目标方法 mi,并告诉 Spring 要在目标方法前后执行哪些方法,这里为了演示效果在目标方法前后分别向控制台输出了相应语句。
3. 创建 Spring 配置文件
在 com.mengma.factorybean 包下创建配置文件 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--目标类 -->
<bean id="customerDao" class="com.mengma.dao.CustomerDaoImpl" />
<!-- 通知 advice -->
<bean id="myAspect" class="com.mengma.factorybean.MyAspect" />
<!--生成代理对象 -->
<bean id="customerDaoProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<!--代理实现的接口 -->
<property name="proxyInterfaces" value="com.mengma.dao.CustomerDao" />
<!--代理的目标对象 -->
<property name="target" ref="customerDao" />
<!--用通知增强目标 -->
<property name="interceptorNames" value="myAspect" />
<!-- 如何生成代理,true:使用cglib; false :使用jdk动态代理 -->
<property name="proxyTargetClass" value="true" />
</bean>
</beans>
上述代码中,首先配置目标类和通知,然后使用 ProxyFactoryBean 类生成代理对象;第 14 行代码配置了代理实现的接口;第 16 行代码配置了代理的目标对象;第 18 行代码配置了需要植入目标的通知;当第 20 行代码中的 value 属性值为 true 时,表示使用 CGLIB 代理,属性值为 false 时,表示使用 JDK 动态代理。
4. 创建测试类
在 com.mengma.factorybean 包下创建一个名为 FactoryBeanTest 的测试类,编辑后如下所示。
package com.mengma.factorybean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mengma.dao.CustomerDao;
public class FactoryBeanTest {
@Test
public void test() {
String xmlPath = "com/mengma/factorybean/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
xmlPath);
CustomerDao customerDao = (CustomerDao) applicationContext
.getBean("customerDaoProxy");
customerDao.add();
customerDao.update();
customerDao.delete();
customerDao.find();
}
}
5. 运行项目并查看结果
使用 JUnit 测试运行 test() 方法,运行成功后,控制台的输出结果如图 1 所示。