0

我被Spring aop困住了。我试图在类中记录一些方法被注释,我已经配置了一个 xml 文件和方面类,但似乎没有任何工作。
aopconfig.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"
           xmlns:context="http://www.springframework.org/schema/context"
           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
                               http://www.springframework.org/schema/context
                               http://www.springframework.org/schema/context/spring-context-2.5.xsd
    ">




     <aop:aspectj-autoproxy/>
     <context:component-scan base-package="BL.AOPLogger" />
  <bean class="BL.TaxiStation" />

            <!-- Aspect -->
         <bean id="AOPLogger" class="BL.AOPLogger"/>






</beans>

LogThis.java

 package BL;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogThis {
    String actionPerformed();
}

AOPLogger.java

    package BL;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;
/**
 *
 * @author PUWKIN
 */
@Component
@Aspect
public class AOPLogger {

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

     @After("anyPublicMethod() && @annotation(LogThis)")
    public void LogCallAfter(JoinPoint JP){

        String methodName = JP.getSignature().getName();
        System.out.println("In Aspect");
        System.out.println(methodName);
    }

}

TaxiStation.java

 package BL;


import java.io.IOException;
import java.io.Serializable;
import java.util.Vector;
//import java.util.logging.*;
import java.util.Calendar;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.Logger;
import BL.LogThis;

import Listeners.PassengersEventsListener;
import Listeners.TaxisEventsListener;
import UI.MainPanel;

public class TaxiStation implements Runnable, Serializable{
    private String Sname;
    private int maxWaitingTaxis;
    private Vector<Taxi> allTaxi =new Vector<Taxi>();
    private Vector<Taxi> awaitingTaxi = new Vector<Taxi>();
    private Vector<Taxi> drivingTaxi = new Vector<Taxi>();
    private Vector<Taxi> finishedDrivingTaxi = new Vector<Taxi>();
    private Vector<Passenger> allPassengers = new Vector<Passenger>();
    private boolean isAvailable = true;
    private boolean isAvailableQue=true;
    private boolean isOpen=true;
    private static Object stationRester = new Object();
    private String sameDist;
    private boolean ReOpened=false;
    private   float StartPrice;
    private  float PricePerSecond;
    private int wait=0;
    private Logger logger;
    transient private TaxisEventsListener listenerTaxi;
    transient private PassengersEventsListener listenerPassenger;
    private boolean fillFirstTime=false;
    private boolean copyData;

        @LogThis(actionPerformed="doSomething")
    public void EndTaxiDriving(int tNumber)
    {
        for (int i=0;i<drivingTaxi.size();i++)
        {
            if (drivingTaxi.elementAt(i).getTnumber()==tNumber)
            {
                drivingTaxi.elementAt(i).EndTaxiDriving();
            }
        }
    }


    public TaxiStation(String Sname, int max,TaxisEventsListener listenerTaxi,PassengersEventsListener listenerPassenger)
    {
        copyData=false;
        this.setSname(Sname);
        this.maxWaitingTaxis=max;
        this.listenerTaxi=listenerTaxi;
        this.listenerPassenger=listenerPassenger;
        //this.openHours=(long) (Math.random() * 100000);
    }


public TaxiStation(TaxiStation taxiStation,TaxisEventsListener listenerTaxi,PassengersEventsListener listenerPassenger)
{
    this.copyData=true;
    this.Sname=taxiStation.Sname;
    this.maxWaitingTaxis=taxiStation.maxWaitingTaxis;
    this.allTaxi=taxiStation.allTaxi;
    this.awaitingTaxi=taxiStation.awaitingTaxi;
    this.drivingTaxi=taxiStation.drivingTaxi;
    this.finishedDrivingTaxi=taxiStation.finishedDrivingTaxi;
    this.allPassengers=taxiStation.allPassengers;
    this.isAvailable =taxiStation.isAvailable;
    this.isAvailableQue=taxiStation.isAvailableQue;
    this.isOpen=taxiStation.isOpen;
    this.sameDist=taxiStation.sameDist;
    this.ReOpened=taxiStation.ReOpened;
    this.StartPrice=taxiStation.StartPrice;
    this.PricePerSecond=taxiStation.PricePerSecond;
    this.wait=taxiStation.wait;
    this.logger=taxiStation.logger;
    this.fillFirstTime=taxiStation.fillFirstTime;
    this.listenerTaxi=listenerTaxi;
    this.listenerPassenger=listenerPassenger;
    registerListeners();
}

void registerListeners()
{
    for (int i=0;i<allPassengers.size();i++)
        allPassengers.elementAt(i).changeListener(listenerPassenger);

    for (int i=0;i<allTaxi.size();i++)
        allTaxi.elementAt(i).changeListener(listenerTaxi);

    for (int i=0;i<drivingTaxi.size();i++)
        drivingTaxi.elementAt(i).changeListener(listenerTaxi);

    for (int i=0;i<awaitingTaxi.size();i++)
        awaitingTaxi.elementAt(i).changeListener(listenerTaxi);

    for (int i=0;i<finishedDrivingTaxi.size();i++)
        finishedDrivingTaxi.elementAt(i).changeListener(listenerTaxi); 
}
@LogThis(actionPerformed="doSomething")
public void  addTaxi(Taxi newTaxi) {
    allTaxi.add(newTaxi);
    //newTaxi.start();  
}

我错过了什么吗?谢谢?

4

1 回答 1

0

注释的引用可能必须与包名称一起使用,您可以试试这个:

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

@After("anyPublicMethod() && @annotation(BL.LogThis)")
...

另一种指定方式是:

@Pointcut("execution(@BL.LogThis public * *(..))")
public void anyPublicMethod() {}

@After("anyPublicMethod()")
...
于 2012-08-21T13:41:52.643 回答