接口
package net.blogjava.dodoma.spring.aop;
public interface HelloI {
public String sayHello(String firstName,String lastName);
}
实现类
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) {
// TODO Auto-generated method stub
log.info("in the class "+this.getClass().getName()+"'s method sayHello()");
return (msg+" "+firstName+" "+lastName);
}
}
BeforeAdvice通知
package net.blogjava.dodoma.spring.aop;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 方法调用之前.
* 先调用此方法
* @author dodoma
**/
public class LogBeforeAdvice implements MethodBeforeAdvice {
protected static final Log log = LogFactory.getLog(LogBeforeAdvice.class);
public void before(Method m, Object[] args, Object target) throws Throwable {
log.info("in the class "+this.getClass().getName()+"'s method before()");
log.info("the target class is:" + target.getClass().getName());
log.info("the target method is:" + m.getName());
for (int i = 0; i < args.length; i++) {
log.info("the method's args is:" + args[i]);
}
//测试,如果在before通知中发生了异常,程序流程将如何
//throw new Exception("异常");
}
}
测试类
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);
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//应用spring的ioc容器
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...");
//如果没有使用spring的ioc,可以直接用如下代码测试
ProxyFactory factory=new ProxyFactory();
factory.addAdvice(new LogBeforeAdvice());//添加通知
factory.setTarget(new Hello("hello"));//添加被代理的类实例
try{
HelloI hi=(HelloI)factory.getProxy();
hi.sayHello("ma","bin");}
catch(Exception e){e.printStackTrace();}
}
}
spring的配置文件beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--享受日志的类-->
<bean id="theTargetBean" class="net.blogjava.dodoma.spring.aop.Hello">
<property name="msg">
<value>hello</value>
</property>
</bean>
<!--advices-->
<bean id="theLogBeforeAdvice" class="net.blogjava.dodoma.spring.aop.LogBeforeAdvice"/>
<!--CONFIG-->
<bean id="theBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--接口-->
<property name="proxyInterfaces">
<value>net.blogjava.dodoma.spring.aop.HelloI</value>
</property>
<!--被代理的类-->
<property name="target">
<ref local="theTargetBean"/>
</property>
<!--加在代理类上的advice-->
<property name="interceptorNames">
<list>
<value>theLogBeforeAdvice</value><!--此时直接使用advice,表明这个类所有的实例,方法,都享受advice的拦截-->
</list>
</property>
</bean>
<!--切入点,可以精确匹配类,方法,也可以不需要这个-->
<!--Note: An advisor assembles pointcut and advice-->
<bean id="theBeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="theLogBeforeAdvice"/>
</property>
<!--匹配模式-->
<property name="pattern">
<value>.*</value>
</property>
</bean>
</beans>
posted on 2006-03-28 12:02
dodoma 阅读(444)
评论(0) 编辑 收藏 所属分类:
spring