Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > AspectJ and Spring AOP


Автор: bytes 23.7.2009, 22:41
AspectJ Don't working. Please help me. Where my mistakes?
Код

package springtestapp1;

import org.aspectj.lang.annotation.*;

@Aspect
public class Audence{
    public Audence(){}

    @Pointcut("execution(* *.performance())")
    public void performance(){
        System.out.println("---Main Method!---");
    }

    @Before("performance()")
    void before(){
        System.out.println("---Before Method---");
    }

    @After("performance()")
    void after(){
        System.out.println("---After Method---");
    }

    public static void main(String[] arg){
        Audence au = new Audence();
        au.performance();
    }
}


Spring AOP don't working too! :(
Код

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="myBean" class="mypackage.MyClass" />

    <aop:aspectj-autoproxy/>
    <aop:config>
        <aop:aspect ref="myBean">
            <aop:before method="myBeforeMethod()" pointcut="execution(* mypackage.*.myMainMethod(..))"/>
        </aop:aspect>
    </aop:config>
</beans>

Код

package mypackage;

/**
 *
 * @author User
 */
public class MyClass{

     public void myMainMethod(){
         System.out.println("This is my mainly method.");
     }


     public void myBeforeMethod(){
         System.out.println("... and this is my before method.");
     }

    public static void main(String[] args){
        ApplicationContext ac = new FileSystemXmlApplicationContext("myBeans.xml");
        MyClass my = null;

        try{
            my = (MyClass)ac.getBean("myBean");

        }
        catch(Exception ex){
            System.out.println(ex.toString());
            System.exit();
        }

         my.myMainMethod();
    }

}

Requirement result:
Код

... and this is my before method.
This is my mainly method.

---------
Only AspectJ with *.aj files works on Netbeans.

Автор: MisterCleric 24.7.2009, 10:42
I think you must declare your aspect class Audence as a bean in spring-config

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)