0

如何从之前的建议中调用建议的方法?我正在使用 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>

4

2 回答 2

1

下面是我尝试测试 spring 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();
    }

}

方面.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.");
    }

}

希望这会帮助你。

于 2012-10-23T07:34:49.390 回答
0

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

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

完成这项工作,然后继续进行更高级的切入点并匹配 AspectJ 表达式。

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

干杯,

于 2012-10-23T07:49:55.347 回答