但是我运行是怎么就不行呢?
只要把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{
/<strong> * */ 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 {
/</strong> * */ 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) { } }
|