接口修改為
package net.blogjava.dodoma.spring.aop;
public interface HelloI {
?public String sayHello(String firstName,String lastName)throws Exception;
?}
類修改為
package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Hello implements HelloI {
?protected static final Log log=LogFactory.getLog(Hello.class);
?private String msg;
?public Hello(){}
?public Hello(String msg){
??this.msg=msg;
?}
?
?public String getMsg() {
??return msg;
?}
?public void setMsg(String msg) {
??this.msg = msg;
?}
?public String sayHello(String firstName, String lastName) throws Exception{
??// TODO Auto-generated method stub
??log.info("in the class "+this.getClass().getName()+"'s method sayHello()");
? throw new Exception("here is a exception !");
??return (msg+" "+firstName+" "+lastName);
?}
}
package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.ThrowsAdvice;
public class LogThrowAdvice implements ThrowsAdvice {
?protected static final Log log = LogFactory.getLog(LogThrowAdvice.class);
?public void afterThrowing(Exception e)throws Throwable{
??log.info("in the class "+this.getClass().getName()+"'s method afterThrowing()");
??log.info("the exception is "+e.getMessage());
?}
}
package net.blogjava.dodoma.spring.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloTest {
?protected static final Log log = LogFactory.getLog(HelloTest.class);
?/**
? * @param args
? * @throws Exception
? */
?public static void main(String[] args) throws Exception {
??// TODO Auto-generated method stub
??Resource rs = new ClassPathResource("beans.xml");
??BeanFactory bf = new XmlBeanFactory(rs);
??HelloI h = (HelloI) bf.getBean("theBean");
??log.info("starting...");
??try {
???log.info(h.sayHello("ma", "bin"));
??} catch (Exception e) {
???e.printStackTrace();
??}
??log.info("end...");
??
??
??ProxyFactory factory=new ProxyFactory();
??factory.addAdvice(new LogThrowAdvice ());
??factory.setTarget(new Hello("hello"));
??try{
??HelloI hi=(HelloI)factory.getProxy();
??hi.sayHello("ma","bin");}
??catch(Exception e){e.printStackTrace();}
?}
}
posted on 2006-03-28 12:57
dodoma 閱讀(243)
評論(0) 編輯 收藏 所屬分類:
spring