想问2个关于spring的aop的问题

05-01-25 sabbath
一、对于异常,只有抛出的才能被通知吗?要是捕捉了(用了try-catch)就不能了吗?

二、spring的aop支不支持嵌套?比如:methodA methodB, methodA调用methodB,我的配置文件对于这2个方法都会做通知,我分别调用的测试过,没问题的,但是如果我用methodA调用methodB,然后我的主程序只调用methodA ,那么就只能捕捉到methodA的通知,methodB就没有了,这是怎么回事?

这是我的配置文件,是不是有什么需要添加的?

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans> 
  <!--CONFIG--> 
  <bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean"> 
    <property name="proxyInterfaces"> 
      <value>com.company.springaop.test.Bean</value> 
    </property> 
    <property name="target"> 
      <ref local="beanTarget"/> 
    </property> 
    <property name="interceptorNames"> 
      <list> 
        <value>beforeAdvisor</value> 
        <value>afterAdvisor</value> 
        <value>nullPointerAdvisor</value>
      </list> 
    </property> 
  </bean> 

  <!--CLASS--> 
  <bean id="beanTarget" class="com.company.springaop.test.BeanImpl"/> 

  <!--ADVISOR--> 
  <!--Note: An advisor assembles pointcut and advice--> 
  <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> 
    <property name="advice"> 
      <ref local="theBeforeAdvice"/> 
    </property> 
    <property name="patterns"> 
      <value>.*Method*</value> 
    </property> 
  </bean> 

  <!--ADVICE--> 
  <bean id="theBeforeAdvice" class="com.company.springaop.test.TestBeforeAdvice"/> 
  
  
  <bean id="afterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> 
    <property name="advice"> 
      <ref local="theAfterAdvice"/> 
    </property> 
    <property name="pattern"> 
      <value>.*Method*</value> 
    </property> 
  </bean>
  
  <bean id="theAfterAdvice" class="com.company.springaop.test.TestAfterAdvice"/> 
  
  <bean id="nullPointerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> 
    <property name="advice"> 
      <ref local="theNullPointerAdvice"/> 
    </property> 
    <property name="pattern"> 
      <value>.*Method*</value> 
    </property> 
  </bean>
  
  <bean id="theNullPointerAdvice" class="com.company.springaop.test.TestThrowAdvice"/> 
  
  
</beans> 
<p>

banq
2005-01-26 13:44
1.使用try catch也要抛出,这是Java Exception语言特性。

2. spring的aop支持嵌套,这是特点。

sabbath
2005-01-26 15:08
但是我运行是怎么就不行呢?

只要把exception catch了,就不发通知了

而且不支持嵌套,我在楼顶的帖子已经说明现象了,我也很纳闷,这么简单的例子,怎么会出错。

以下是代码。可能和我顶贴有出入,因为我再改,但是测试的没错,现象我已经说过,我也很纳闷

这是Bean接口
package com.company.springaop.test;

import java.sql.SQLException;

/*
 * 
 */
public interface Bean {
    public void theMethod()throws SQLException;
    public void secondMethod() throws SQLException;
}


这是BeanImpl,实现Bean接口
package com.company.springaop.test;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;


/*
 * 
 */
public class BeanImpl implements Bean{

    /**
     * 
     */
    public BeanImpl() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void secondMethod() throws SQLException{
        //try {
            theMethod();
        //} catch (SQLException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        //}
    }

    public void theMethod() throws SQLException { 
        System.out.println(this.getClass().getName() 
            + "." + new Exception().getStackTrace()[0].getMethodName() 
            + "()" 
            + " says HELLO!"); 
        try{
            throw new NullPointerException();
        }catch(NullPointerException e){
            
        }
        throw new SQLException();
      } 
    public static void main(String[] args) throws FileNotFoundException, SQLException { 
        //Read the configuration file 
//        ApplicationContext ctx 
//            = new FileSystemXmlApplicationContext("src/springconfig.xml"); 
        //Resource source = 
        //InputStream is = new FileInputStream("src/springconfig.xml");
        Resource rc = new ClassPathResource("springconfig.xml");

        BeanFactory factory = new XmlBeanFactory(rc);

        //Instantiate an object 
        Bean x = (Bean) factory.getBean("bean");
        
        //Execute the public method of the bean (the test) 
        x.secondMethod();//这样就不能对theMethod方法做通知了,只对secondMethod通知了。
        //x.theMethod();//如果这样,就可以对theMethod做通知
        
        
      } 

}

这是AfterReturningAdvice 

package com.company.springaop.test;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

/*
 * 
 */
public class TestAfterAdvice implements AfterReturningAdvice {

    /**
     * 
     */
    public TestAfterAdvice() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        System.out.println("after!!!");

    }
    public static void main(String[] args) {
    }
}

这是TestBeforeAdvice
package com.company.springaop.test;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

/*
 *  
 */
public class TestBeforeAdvice implements MethodBeforeAdvice {

    public void before(Method m, Object[] args, Object target) throws Throwable {
        System.out.println(m.getName());
        System.out.println("Hello world! (by " + this.getClass().getName()
                + ")");
    }

}


这是TestThrowAdvice
package com.company.springaop.test;

import java.lang.reflect.Method;
import java.sql.SQLException;

import org.springframework.aop.ThrowsAdvice;

/*
 * 
 */
public class TestThrowAdvice implements ThrowsAdvice {

    
    
    public void afterThrowing(Method m, Object[] args, Object target,SQLException ex) throws Throwable {
        System.out.println("thorw a SQLrException()");
    }
    
    public static void afterThrowing(Method m, Object[] args, Object target, NullPointerException ex) {
        System.out.println("thorw a NullPointerException()");
    }


    public static void main(String[] args) {
    }
}
<p>

猜你喜欢