3

の呼び出しの前にコードを織り込もうとしていますstart();

これは私がアドバイスしたいTestClassです:

package com.test;

public class TestClass {

    public static void main(String[] args) {

        new TestClass().start();
    }
    private void start() {

        System.out.println("Test started");
    }
}

これはアドバイスを含む側面です:

package com.test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

    @Before("call(void com.test.TestClass.start())")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("logBefore() is running");
    }

}

これは私のspring.xmlです:

    <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/>
<context:annotation-config/>
<context:component-scan base-package="com.test"/>

また、次のようにXMLで明示的にBeanに名前を付けようとしました。

<aop:aspectj-autoproxy/>
<bean id="test" class="com.test.TestClass" />
<bean id="aspect" class="com.test.LogAspect" />

これは私のプロジェクトのセットアップです(私はSpring Tool Suiteバージョン3.1.0を使用しています):

プロジェクトの設定

その結果、TestClass.startは問題なく呼び出されますが、アドバイスは適用されません。

アドバイスが適用されるように何を変更する必要がありますか?

ありがとう。

編集:私はついにそれを機能させました:

あなたの提案に従って私のコードを編集した後、私はこのチュートリアルを見ました。これにより、TestClassにインターフェイスを実装させることができました。そしてそれは問題を修正しました。

これが私の最終設定です:

最終設定

そのインターフェースを含むTestClass:

package com.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

interface TestClass {
    void start();
}

public class TestClassImpl implements TestClass {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
                "Spring-Config.xml");

        TestClass t1 = (TestClass) appContext.getBean("myTest");
        t1.start();
    }

    @Override
    public void start() {
        System.out.println("test");
    }
}

側面:

package com.test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LogAspect {
    @Before("execution(void com.test.TestClass.start(..))")
    public void logBefore(JoinPoint joinPoint) {

        System.out.println("logging before "
                + joinPoint.getSignature().getName());
    }
}

そしてSpring-Config.xml

<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/>    
    <bean id="myTest" class="com.test.TestClassImpl" />
    <bean id="logAspect" class="com.test.LogAspect" />

</beans>

ご協力いただきありがとうございます。

4

1 に答える 1

2

メソッドが別のクラスから呼び出されるとcall、AspectJ ポイントカットが適用されます。これを自分で直接呼び出しているため、実行ポイントカットを使用する必要があるため、次のように変更します。

@Before("call(void com.test.TestClass.start())")

@Before("execution(void com.test.TestClass.start())")

またSpring、Bean を自分で直接作成するのではなく、作成させてください。


余談:注: SpringXML ファイルをソース ファイルとは別に保持するsrc/resourcesと、 によって取得されClassPathXmlApplicationContextます。

于 2012-12-28T15:24:44.403 に答える