论坛首页 Java企业应用论坛

spring 小问题,那个高手帮忙看哈~~~~~~~

浏览 1015 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2008-02-20  
AOP中自己写了一个切点,还有一个方法前通知,其中代码是这样的:
前置通知:
package advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class beforeadvice implements MethodBeforeAdvice {

public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
// TODO Auto-generated method stub

System.out.println(arg2.getClass().getName()+"----"+arg0.getName()+"----"+"前置增强完成");
}

}
切点:
package advice;

import java.lang.reflect.Method;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;

public class greetingadvisor extends StaticMethodMatcherPointcutAdvisor {

public boolean matches(Method arg0, Class arg1) {
// TODO Auto-generated method stub

return "greetto".equals(arg0.getName());
}

}
目标类:
package advice;

public class naivewaiter implements waiter {

public void greetto(String name) {
// TODO Auto-generated method stub
  System.out.println("greet to "+name);

}

public void serveto(String name) {
//TODO Auto-generated method stub
   System.out.println("serving "+name);
}

}

配置文件:
<bean id="beforeAdvice" class="advice.beforeadvice"></bean>
<bean id="greetingadvisor" class="advice.greetingadvisor" >
<property name="advice" ><ref bean="beforeAdvice"/></property>
</bean>
<bean id="nwaiter" class="advice.naivewaiter"></bean>

<bean id="waiter"  class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="proxyInterfaces">
<value>advice.waiter</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
</list>
</property>
<property name="target" ref="nwaiter"/>
</bean>
目标类接口:
package advice;

public interface waiter {
void greetto(String name);
void serveto(String name);
}
主函数:
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//sell sell1=(sell)ctx.getBean("s");
waiter w=(waiter)ctx.getBean("waiter");

w.greetto("yinfan");
w.serveto("xiaoqiang")
问题1:
为什么我这样一个方法前通知结果打印了三跳语句啊?
结果是:
$Proxy0----greetto----前置增强完成
$Proxy0----greetto----前置增强完成
advice.naivewaiter----greetto----前置增强完成
greet to yinfan
serving xiaoqiang
问题2:
waiter w=(waiter)ctx.getBean("waiter");
这里的waiter 是接口,我换成实现接口的类就不行了?这个是为什么呢?是不是这里必须要用接口啊?
问题3:
配置文件中全部都用的JDK代理,要是我换成CGLIB代理就说finanl不能继承~~~~~~~这个是为什么呢?
希望高手帮我看哈嘛 谢谢~~~~~~~~
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics