4

私のアスペクトクラスは、

@Configuration
@EnableAspectJAutoProxy
@Component
@Aspect
public class AspectClass {

    @Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())")
    public void logBefore(JoinPoint joinPoint) {

        System.out.println("Before running the beforeAspect() in the AopTest.java class!");
        System.out.println("Hijacked Method name : " + joinPoint.getSignature().getName());
        System.out.println("************************");
    }

}

私の他のJavaクラス

public class AopTest {

    public void beforeAspect() {
        System.out.println("This is beforeAspect() !");
    }
}

私のメインクラスは

public class MainMethod {

    public static void main(String[] args) {    
        ApplicationContext context = new FileSystemXmlApplicationContext("ApplicationContext/applicationContext.xml");
        AopTest test = (AopTest)context.getBean("bean1");
        test.beforeAspect();
    }
}

私の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" 
    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 ">

    <bean id="bean1" class="com.pointel.aop.test1.AopTest" />

</beans>

この@Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())")inは、 Main メソッドの実行時に のAspectClass前に実行されません。beforeAspect()AopTest

良い答えは間違いなく高く評価されます。

4

4 に答える 4

4

まず、アノテーション ベースの構成を使用する場合は、AnnotationConfigApplicationContext代わりにFileSystemXmlApplicationContext. applicationContext.xmlファイルを削除して、構成クラスにメソッドを追加するだけです@Bean。このようなもの:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "your.aspect.package")
public class AspectConfig {
    @Bean 
    public AopTest aopTest() {
        return new AopTest();
    }
}

あなたのメインで

public class MainMethod {

    public static void main(String[] args) {    
        AnnotationConfigApplicationContextcontext = new AnnotationConfigApplicationContext(AspectConfig.class);
        // don't forget to refresh
        context.refresh();
        AopTest test = (AopTest)context.getBean("aopTest");
        test.beforeAspect();
    }
}

にはAspectClassが必要@Component@Aspect、メソッドには のようなアドバイスまたはポイントカット アノテーションが必要@Beforeです。@ComponentSpring がスキャンすることを認識できるように、 である必要があります。

于 2013-03-27T15:10:13.203 に答える
1

ここで、注釈を使用するためにいくつかのコードを xml に追加する必要があります-1.for @component 注釈。

xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" 

2.その後、コンポーネント スキャンを使用して、@component アノテーションを使用し、aop autoproxy を使用するすべてのアノテーション付き Bean クラスを取得します。

<context:annotation-config/>
<context:component-scan base-package="mypackage"></context:component-scan>
 <aop:aspectj-autoproxy>
</aop:aspectj-autoproxy>

例については、www.technicaltoday.com/p/spring.html をご覧ください

于 2013-03-27T15:58:59.677 に答える
0

AspectClassBean 構成にあなたが表示されません。また、Bean として宣言する必要があります。

于 2015-07-31T13:44:49.183 に答える
0

アスペクト クラスにポイント カットの定義がありません。

例えば;

@Pointcut("execution(* *.advice(..))")  
public void logBefore(){}  

@Before("logBefore()")  
public void beforeAdvicing(){  
System.out.println("Listen Up!!!!");  
}  

まず、アスペクトを織り込むポイントを定義する必要があります。これは、ポイント カットを使用して行います。これは、@Before アノテーション内で指定するポイント カット名です。詳細については、私のブログ投稿 @ http://dinukaroshan.blogspot.com/2010/06/aop-with-spring.htmlをご覧ください。

于 2013-03-27T16:45:21.093 に答える