2017-09-04 10 views
0

私はプロキシオブジェクトを取得するにはProxyFactoryBeanを使用する場合、私はにClassCastExceptionを得るが、私はプロキシオブジェクトを取得するにはProxyFactoryのgetProxy()を使用する場合、それが正常に動作します。私はSpring 4.xを使用します。春量Aop ProxyFactoryBeanとProxyFactoryとClassCastExceptionが

2つのBean、WaiterTestと売主の

定義:アドバイスの

public class WaiterTest { 
    public void greetTo(String name){ 
     System.out.println("waiter greet to " + name +"..."); 
    } 
    public void serveTo(String name){ 
     System.out.println("waiter serving " + name + "..."); 
    } 
} 

public class Seller { 
    public void greetTo(String name){ 
     System.out.println("seller greet to " +name + "..."); 
    } 
} 

定義:顧問の

public class GreetingBeforeAdvice implements MethodBeforeAdvice{ 

    public void before(Method method, Object[] args, Object obj) throws Throwable{ 
     System.out.println(obj.getClass().getName() + "." + method.getName()); 
     String clientName =(String) args[0]; 
     System.out.println("How are you! Mr." + clientName +"."); 
    } 
} 

定義:

public class GreetingAdvisor extends StaticMethodMatcherPointcutAdvisor { 
    public boolean matches(Method method, Class clazz) { 
     return "greetTo".equals(method.getName()); 
    } 

    public ClassFilter getClassFilter() { 
     return new ClassFilter() { 
      public boolean matches(Class clazz) { 
       return WaiterTest.class.isAssignableFrom(clazz); 
      } 
     }; 
    } 
} 

テストクラス:

public class TestGreetingBeforeAdvisor { 
     public static void main(String[] args) { 
      //method one: use by ProxyFactory 
      WaiterTest targetWaiterTest = new WaiterTest(); 
      Seller targetSeller = new Seller(); 

      GreetingBeforeAdvice advice = new GreetingBeforeAdvice(); 
      GreetingAdvisor advisor = new GreetingAdvisor(); 
      advisor.setAdvice(advice); 

      ProxyFactory pf = new ProxyFactory(); 
      pf.setTarget(targetWaiterTest); 
      pf.addAdvisor(advisor); 
      pf.setOptimize(true); 
      WaiterTest proxy = (WaiterTest) pf.getProxy(); 
      proxy.greetTo("John"); 
      proxy.serveTo("Tom"); 

      ProxyFactory pf1 = new ProxyFactory(); 
      pf1.setTarget(targetSeller); 
      pf1.addAdvisor(advisor); 
      Seller seller = (Seller) pf1.getProxy(); 
      seller.greetTo("John"); 
      System.out.println("============="); 


      //method two:Spring xml,use ProxyFactoryBean 
      ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
       WaiterTest test = (WaiterTest) ctx.getBean("waiterTest"); 
     test .greetTo("John"); 
     test .serveTo("John"); 
    } 
} 

beans.xmlの:

<bean id="waiterTarget" class="com.xxx.springaop.advisor.WaiterTest"/> 
    <bean id="sellerTarget" class="com.xxx.springaop.advisor.Seller"/> 
    <bean id="greetingAdvice1" class="com.xxx.springaop.advisor.GreetingBeforeAdvice"/> 
    <bean id="greetingAdvisor" class="com.xxx.springaop.advisor.GreetingAdvisor" 
     p:advice-ref="greetingAdvice1"/> 
    <bean id="parent" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean" 
     p:interceptorNames="greetingAdvisor" 
     p:proxyTargetClass="true"/> 

    <bean id="waiterTest" parent="parent" p:target-ref="waiterTarget"/> 
    <bean id="seller" parent="parent" p:target-ref="sellerTarget"/> 

結果:

com.xxx.springaop.advisor.WaiterTest.greetTo 
How are you! Mr.John. 
waiter greet to John... 
waiter serving Tom... 
seller greet to John... 
============= 
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy12 cannot be cast to com.xxx.springaop.advisor.WaiterTest 
    at com.xxx.springaop.advisor.TestGreetingBeforeAdvisor.main(TestGreetingBeforeAdvisor.java:48) 

要約したものです。

WaiterTest proxy = (WaiterTest) pf.getProxy(); //success 
WaiterTest test = (WaiterTest) ctx.getBean("waiterTest");//fail,ClassCastException 

なぜ?

+0

をすべてうまくいくようですか? –

答えて

0

私はあなたの問題を再現できませんでしたが、あなたのコードでいくつかのリファクタリングを行いましたが、これはうまく動作します。多分それが出発点として、少なくとも同様にあなたを助けます...

Waiter.java

public class Waiter { 
    public void greetTo(String name) { 
     System.out.println("waiter greet to " + name + "..."); 
    } 

    public void serveTo(String name) { 
     System.out.println("waiter serving " + name + "..."); 
    } 
} 

GreetingBeforeAdvice.java

import org.springframework.aop.MethodBeforeAdvice; 

import java.lang.reflect.Method; 

public class GreetingBeforeAdvice implements MethodBeforeAdvice { 

    @Override 
    public void before(Method method, Object[] args, Object obj) throws Throwable { 
     String clientName = (String) args[0]; 
     System.out.println("How are you! Mr." + clientName + "."); 
    } 
} 

GreetingAdvisor.java

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

import java.lang.reflect.Method; 

public class GreetingAdvisor extends StaticMethodMatcherPointcutAdvisor { 

    public boolean matches(Method method, Class clazz) { 
     return "greetTo".equals(method.getName()); 
    } 

    public ClassFilter getClassFilter() { 
     return new ClassFilter() { 
      public boolean matches(Class clazz) { 
       return Waiter.class.isAssignableFrom(clazz); 
      } 
     }; 
    } 
} 

beans.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="waiter" class="Waiter"/> 

    <!-- Advisor Configuration --> 
    <bean id="greetingAdvice" class="GreetingBeforeAdvice"/> 
    <bean id="greetingAdvisor" class="GreetingAdvisor"> 
     <property name="advice" ref="greetingAdvice"/> 
    </bean> 

    <bean id="waiterProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> 
     <property name="interceptorNames"> 
      <list> 
       <value>greetingAdvisor</value> 
      </list> 
     </property> 
     <property name="target" ref="waiter"/> 
    </bean> 
</beans> 

し、最終的にApp.java

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class App { 

    public static void main(String[] args) { 
     ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
     final Waiter waiter = (Waiter) ctx.getBean("waiterProxyFactory"); 
     waiter.greetTo("Koray Tugay"); 
     waiter.serveTo("Koray Tugay"); 
    } 
} 

春4.3.12使用して、次のように私のための出力は次のようになります。私にとって

How are you! Mr.Koray Tugay. 
waiter greet to Koray Tugay... 
waiter serving Koray Tugay...