Spring aop(四)--@AspectJに基づく


Springでaopを使用する場合、横断論理をターゲットクラスに織り込まないすべての方法を実現するために、あるクラスを位置決めするために接点を使用する方法がある.しかし、接点だけではAOPを実現することができない場合があり、横断論理(通常の通知)が必要であるため、断面にはこれらの情報が含まれる.切断面を作成する方法は様々であるが、@AspectJ方式による構成が最も簡単であるべきである.
1.ターゲットクラスをBeanに登録する
2.接面クラスの登録(接点、横断論理を含む)
3.自動エージェント作成器の登録(@Aspect注記のあるクラスを解析するために使用)
义齿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:context="http://www.springframework.org/schema/context"
    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">
    <bean id="forumServiceImpl" class="org.exam.aop.ForumServiceImpl" />
    <bean id="forumServiceAspect" class="org.exam.aop.ForumServiceAspect" />
    <bean id="annotationAwareAspectJAutoProxyCreator" class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
</beans>
ForumServiceImplではForumServiceインタフェースを実装する必要はありません.annotationAwareAspectJAutoProxyCreatorは自動エージェント作成器です.ForumServiceAspectの内容は次のとおりです.
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class ForumServiceAspect {
	@Before("execution(* removeTopic(..))")
	public void beforeRemoveTopic(){
		System.out.println("beforeRemoveTopic");
	}
}
,を用いて@Aspectクラスを解析することができる.内部には実質的に
<?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">
    <aop:aspectj-autoproxy />
    <bean id="forumServiceImpl" class="org.exam.aop.ForumServiceImpl" />
    <bean id="forumServiceAspect" class="org.exam.aop.ForumServiceAspect" />
</beans>
java-config方式のspring構成を使用する場合、@EnableAspectJAutoProxyを使用するとに相当します.
接点式について(少し複雑です):
1.関数
A.メソッド接点:a.execution()は、あるマッチングモードを満たすすべてのターゲットクラスメソッド接続点を表す.b.@annotation()は、特定の注釈を付したターゲットメソッド接続点を表す.
B.メソッドエントリ:a.args()ターゲットクラスメソッドの実行時にエントリオブジェクトのタイプ定義の意味指定接続点を判別する;@args()は、特定の注釈を付する接続点を指定するか否かを示す.
C.ターゲットクラス接点:a.within()は特定ドメイン下の全ての接続点を表す.b.@within();c.target()ターゲットクラスが指定したクラスにタイプ別に一致する場合、ターゲットクラスのすべての接続点がこの接点に一致する.d.@target();
D.エージェントクラス接点:this()
2.ワイルドカード
*任意の文字に一致するが、コンテキスト内の1つの要素にのみ一致する.
..任意の文字を一致する、コンテキスト内の複数の要素を一致させることができる.クラスを表すときは、*と併用します.入参時に単独で使用することを表す.
+は、指定するクラスにタイプが一致するすべてのクラスを表し、クラス名の後に続く必要がある.
注意点:
a.すべてのワイルドカードをサポート:execution()、within()
b.+ワイルドカードのみサポート:args()、target()、this()
c.ワイルドカード:@args(),@within(),@target(),@annotation()はサポートされていません.
接点関数間では論理演算も可能です.非