我无法使用Spring AOP调用建议的方法

时间:2012-10-23 05:40:09

标签: java spring aop spring-aop

如何从之前的建议中调用建议的方法?我正在使用Spring 3.0.5和XML配置。

我写了以下代码:

public interface Arithmatic {

    public int add(int a,int b);//Advised method
    public int sub(int a,int b);//Advised method
}

public class ArithmaticImpl implements Arithmatic {


   public int add(int a,int b){
       return (a+b); 
   }


   public int sub(int a,int b){
      return (a-b); 
   }
}

我的AOP课程是

public class ValidateNumber{
   public validateNumber(int a,int b){

   if (a<0||b<0)  {
       // how can show validation message?
   } else {
       //how can i call Advised methods?
   }

 }

}

祝贺文件

<aop:config>
    <aop:aspect ref="validatenumber">
    <aop:before pointcut="execution(* com.arithematic.Arithmatic.add(int,int)) and args(a,b)" method="add" arg-names="a,b"/>
    </aop:aspect>
 </aop:config>

2 个答案:

答案 0 :(得分:1)

下面是我试图测试弹簧AOP的示例。

<强> SpringConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                           ">

<bean id="beanPostProcessor" class="springTest.SpringBeanPostProcessor" />
<bean id="contestPostProcessor" class="springTest.SpringPostProcessor" />

<bean id="emp" class="springTest.Employee">
    <property name="id" value="1"/>
    <property name="name" value="sunil"/>
</bean>

<bean id="loginCheckAspect" class="springTest.Aspect" init-method="initAspect"/>

<bean id="msg" class="springTest.LoadMessages" >
    <property name="id" value="1" />
</bean>

<aop:config>
    <aop:pointcut id="login" expression="execution(* *.showMsg*(..))"/>

<aop:aspect id="loginAspect" ref="loginCheckAspect" >
        <aop:before method="checkLogin" pointcut-ref="login" />
    </aop:aspect>

</aop:config>

</beans>

<强> LoadMessages.class

package springTest;

public class LoadMessages {
    int id; 
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public LoadMessages() {
    }

    public void showMsg(){
        System.out.println("#################################################");
        System.out.println("Welcome neel");
        System.out.println();
    }

    public void getInfo(){
        System.out.println("#################################################");
        System.out.println("Welcome neel");
        System.out.println();
    }

}

<强> Aspect.java

package springTest;
import java.io.DataInputStream;
import java.io.IOException;


public class Aspect {
    public void checkLogin() throws IOException{
        DataInputStream ds=new DataInputStream(System.in);
        //BufferedInputStream bf=new BufferedInputStream(System.in);
        System.out.println("\n\n** Credential Check Point using AOP login aspect ** ");
        System.out.print("Please login to view employee details");
        System.out.print("\nUser Name : ");
        String userName=ds.readLine();
        String pass;
        if(userName!=null && userName.equals("neel")){
            System.out.print("Password : ");
            pass=ds.readLine();
            if(pass!=null && pass.equals("77")){
                System.out.println();
            }else{
                throw new IOException("Invalid Password");
            }

        }else{
            throw new IOException("Invalid User Name");
        }
    }

    public void initAspect(){
        //System.out.println("@@@@@@  Init method of aspect  @@@@@");
    }

}

<强> SpringBeanPostProcessor.java

package springTest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;


public class SpringBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessAfterInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out.println("Initialization Completes "+arg1);
        return arg0;
    }

    public Object postProcessBeforeInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out.println("Initialization....");
        return arg0;
    }

}

<强> SpringPostProcessor.java

package springTest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;


public class SpringPostProcessor implements BeanFactoryPostProcessor {

    public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
            throws BeansException {
        System.out.println("Initialization Context Succssefull.");
    }

}

希望这会对你有所帮助。

答案 1 :(得分:0)

为什么不首先尝试更简单的方法:

<aop:config>
    <aop:pointcut id="mypointcut" expression="execution(* com.arithematic.*.*(..))" />
    <aop:advisor advice-ref="validatenumber" pointcut-ref="mypointcut" />
</aop:config>

让它工作,然后转到更高级的切入点并匹配AspectJ表达式。

(另外:你在你的类中将“算术”拼写为“Arithmatic”,在包名中拼写为“arithematic” - 确保这些在XML和代码之间实际匹配,以排除导致你的方面不起作用的简单拼写错误: - )

干杯,