記事のディレクトリ
AOPとは何ですか?
-
、アスペクト指向プログラミングであるAOP(アスペクト指向プログラミング)を、添加し、改善された(オブジェクト指向プログラミング、オブジェクト指向プログラミング)OOPと言うことができます。OOPは、共通セットの動作をシミュレートするために使用されるオブジェクトの階層構造を構築するなど、カプセル化、継承、ポリモーフィズム、概念として導入しました。しかし、OOPは、開発者が上下関係を定義するが、例えば、対数関数、定義された横方向の関係に適していないことを可能にします。ログコードはすべてのオブジェクト階層に横方向に、機能に対応する中核オブジェクトは、セキュリティ、例外処理、および連続のようなコードの他のタイプには関係が、透明そうではないが拡散する傾向があり、このスプレッド独立したコードは、再利用可能なモジュールを犠牲にし、コードの繰り返しの多くにつながったオブジェクト指向設計において、横(横断)を通して呼ばれます。
-
「反して「横」と呼ばれる技術を使用していますAOP技術、内部断面デカプセル化オブジェクト、および再利用可能な複数のモジュールの中にカプセル化された公共のクラスの動作に影響を与えるもの、そしてそれを命名アスペクト」、すなわちセクション。簡単に重複したコードには、いわゆる「カット」、単にビジネスを行うためのものは何もありませんが、それはビジネスロジックやカプセル化と呼ばれる共通モジュールの責任である、システムを削減し、モジュール間の結合度を低減し、かつ将来の操作性を容易にするために、および保守性。
-
コアの懸念と横断的関心事:技術を「クロスカット」使用は、AOPは、ソフトウェアシステムは、2つの部分に分かれています。ビジネスプロセスの主な流れは関係のない部分は、横断的関心事であり、コアの懸念です。横断的関心事は、彼らはしばしば、多くのコアの懸念に発生し、かつ、そのような認証局、ログものなど、どこにでも本質的に同様であることを特徴。
-
AOPは、様々な問題分離システム、コア及び横断的関心事を分離することです。春AOPモジュールは、アプリケーションをインターセプトするインターセプタを提供する方法を実行する場合、例えば、あなたは実行方法の前または後に機能を追加することができます。
AOPの用語
- クロスカットの懸念:これらの懸念は懸念を横断的と呼ばれるインターセプトする方法、傍受に対処する方法を、
- セクション(態様):クラスは、抽象オブジェクトの特徴である、横断的関心事の抽象的部分であります
- 接続点(ジョインポイント):春のみ接続点型方法をサポートするためのポイントは、それがポイントであり、傍受されたスプリングで傍受する接続を意味し、実際には、結合点は、フィールドまたはコンストラクタであってもよいです
- エントリポイント(ポイントカット):傍受の接続点を定義します
- 通知(アドバイス):インターセプトを実行する後に、いわゆる通知は、通知は、5つの通知に囲まれた、前部、後部、異常終了に分割され、接続点コードを指すを指し
- 対象:対象オブジェクト演技
- (織物)織り:セクションでは、ターゲットオブジェクトに適用されたプロキシオブジェクトに至るプロセスが作成します
- (導入)を導入する:コードを変更しないという前提の下で、いくつかのメソッドまたはフィールドの導入は、実行時に動的にクラスに追加することができます
春のセクションでは、通知の5種類に適用することができ
- ターゲットメソッドが呼び出される前に呼び出し通知:(前に)事前通知
- 着信通知を完了ターゲットメソッドの後(正常または異常終了のどちらかをすべてのコール):(後)アドバイスを返送した後、
- (アフター戻っ)戻り通知:ターゲット・メソッドが正常に実行された後の呼び出し通知
- 例外通知(アフター投げ):ターゲットメソッドは例外をスロー呼通知
- アドバイス(周り)の周り:メソッドを呼び出し、コールの行動が通知される前に、通知方法は、通知されるようにした後、カスタムの実装包まれました
春AOPのサポート
-
SpringのIoCコンテナ春AOPプロキシが生成、管理を担当し、その依存関係もIOCコンテナによって管理する責任があります。したがって、AOP剤は、この関係は、依存性注入IOCコンテナを提供することができる、ターゲット容器のような他のBeanインスタンスを直接使用することができます。春はエージェントのルールを作成します。
- プロキシことのいずれかのインターフェイスのインスタンスを作成することができるように1、デフォルトで動的プロキシAOPプロキシを作成するためにJavaを使用します
- 図2は、クラスは、プロキシインターフェイスエージェントを必要としない場合、スプリングCGLIBもCGLIBを強制するために使用することができる、プロキシを使用してに切り替わり
-
AOPプログラミングは実際には非常に単純なこと、AOPプログラミングを見ている、プログラマは三つの部分に参加する必要は:
- 1、一般的なビジネス・コンポーネントの定義
- 2、出発点の定義は、出発点は、ビジネス・コンポーネントの複数の交差してもよいです
- 3、定義強調処理は、演算処理における強調処理は、コンポーネントが通常のトラフィックに織り込まれているフレームであるAOP
-
キーは、エントリ・ポイントと定義AOP強調処理プログラムを定義エントリポイント一度、適切な強調処理を定義することであるので、AOP AOPプロキシフレームが自動的に生成され、すなわち:プロキシ・オブジェクトの処理方法は、メソッド= +エンハンスメントプロキシオブジェクトです。
春AOPベースのXMLスキーマ
- 春AOPの.xmlファイルテンプレートと呼ばれるaop.xml
- ファイルapplicationContext.xmlをの中SSMフレームで、AOPの設定ファイル
サンプルコード - Logging.java
package com.tutorialspoint;
public class Logging {
public void beforeAdvice(){
System.out.println("前置通知");
}
public void afterAdvice(){
System.out.println("后置通知");
}
public void afterReturningAdvice(Object retVal){
System.out.println("返回通知:" + retVal.toString() );
}
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("异常通知: " + ex.toString());
}
}
- Student.java
package com.tutorialspoint;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age );
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name );
return name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
- MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
Student student = (Student) context.getBean("student");
student.getName();
System.out.println("--------------------");
student.getAge();
System.out.println("--------------------");
student.printThrowException();
}
}
- aop.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:config>
<!--aspect 是使用元素声明的,支持的 bean 是使用 ref 属性引用的-->
<aop:aspect id="log" ref="logging">
<!--切入点-->
<aop:pointcut id="selectAll" expression="execution(* com.tutorialspoint.*.*(..))"/>
<!--前置通知-->
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
<!--后置通知-->
<aop:after pointcut-ref="selectAll" method="afterAdvice"/>
<!--返回通知-->
<aop:after-returning pointcut-ref="selectAll"
returning="retVal"
method="afterReturningAdvice"/>
<!--异常通知-->
<aop:after-throwing pointcut-ref="selectAll"
throwing="ex"
method="AfterThrowingAdvice"/>
</aop:aspect>
</aop:config>
<!-- Definition for student bean -->
<bean id="student" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<!-- Definition for logging aspect -->
<bean id="logging" class="com.tutorialspoint.Logging"/>
</beans>
- 以下に示すように、コード、結果を実行
春AOPベースの注釈モード
サンプルコード
- Logging.java
package com.tutorialspoint1;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
@Aspect
public class Logging {
@Pointcut("execution(* com.tutorialspoint1.*.*(..))")
private void selectAll(){}
@Before("selectAll()")
public void beforeAdvice(){
System.out.println("前置通知");
}
@After("selectAll()")
public void afterAdvice(){
System.out.println("后置通知");
}
@AfterReturning(pointcut = "selectAll()", returning="retVal")
public void afterReturningAdvice(Object retVal){
System.out.println("返回通知:" + retVal.toString() );
}
@AfterThrowing(pointcut = "selectAll()", throwing = "ex")
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("异常通知: " + ex.toString());
}
}
- Student.java
package com.tutorialspoint1;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age );
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name );
return name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
- MainApp.java
package com.tutorialspoint1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop1.xml");
Student student = (Student) context.getBean("student");
student.getName();
System.out.println("--------------------");
student.getAge();
System.out.println("--------------------");
student.printThrowException();
}
}
- aop1.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<!-- Definition for student bean -->
<bean id="student" class="com.tutorialspoint1.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<!-- Definition for logging aspect -->
<bean id="logging" class="com.tutorialspoint1.Logging"/>
</beans>
- 以下に示すように、コード、結果を実行
多くを学ぶために
プロキシ力にCGLIBの使用を生成します
- 春は動的プロキシまたはプロキシの使用は春のCGLIB定期的に、高いバージョンが自動的に動的プロキシまたはプロキシCGLIB生成されたコンテンツを使用するかどうかを選択します生成される前に、当然のことながら、我々はまた、そのプロキシ力にCGLIBの使用を生成することができたのconfig:AOP内部値がtrueに設定されている場合は、プロキシ・ターゲット・クラスがfalseに設定するか、このプロパティを省略した場合、「プロキシターゲット-class」属性を持って、この関数は、エージェントベースのインタフェース、エージェントクラスに基づいて行われますそれは動作します。
テキストのサンプルコードします。https://github.com/Carroll-guogaoqin/Spring-AOP.git
より多くのあなたが知っている、より多くのあなたは知りません。
手術をせずに適切な方法は、患者はまだ手術を終了し、手術する方法はありません、求めることができます。
あなたが他の質問がある場合は、ウェルカムメッセージ、我々は、議論一緒に学び、一緒に進行することができます