2012-11-19 19 views
6

私は豆を使ってオブジェクトを作成するためにスプリングを使用していました。今では、同じオブジェクトを作成するためにaopを使用しようとしましたが、$ ProxyはSaleRoom例外にキャストできません。

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" 
xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd" 
xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config- 1.0.xsd" 
xmlns:jm s="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd" 
xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd" 
xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd" 
xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd" 
xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" 
xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd" 
xmlns:p="http://www.springframework.org/schema/p" 

xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-2.5.xsd 
http://www.springframework.org/schema/context/spring-context-2.5.xsd  http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-2.5.xsd 
http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd  http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-2.5.xsd 
http://www.springframework.org/schema/jms/spring-jms-2.5.xsd  http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-2.5.xsd 
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd  http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-2.5.xsd 
http://www.springframework.org/schema/lang/spring-lang-2.5.xsd  http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-2.5.xsd 
http://www.springframework.org/schema/osgi/spring-osgi.xsd  http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-2.5.xsd 
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-2.5.xsd 
http://www.springframework.org/schema/util/spring-util-2.5.xsd  http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-2.5.xsd 
"> 
<bean id="sale01" class="application.common.entities.BidRoom"> 
<property name="itemId" value="0001"/> 
<property name="lifeTime" value="15"/> 
</bean> 
</beans> 

そして私は販売を作成するには、次のコードを使用:

前のXMLでした

ApplicationContext context = new FileSystemXmlApplicationContext(SalesManager.getSalesSourceFile()); 
    SaleRoom saleRoom; 
    List<String> salesNames = new LinkedList<String>(); 
    List<SaleRoom> allSales = new LinkedList<SaleRoom>(); 

    // Get all sales id's for beans 
    NodeList salesNodeList = salesDoc.getElementsByTagName("bean"); 

    for (int i = 0; i < salesNodeList.getLength(); i++) { 
     Node nNode = salesNodeList.item(i); 
     salesNames.add(((Element) nNode).getAttribute("id").toString()); 
    } 

    for (String saleName : salesNames) { 
     if(saleName.contains("sale")) { 
      saleRoom = (SaleRoom) context.getBean(saleName); 
      allSales.add(saleRoom); 
     } 
    } 

    return allSales; 

これは、新しい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" 
      xmlns:aop="http://www.springframework.org/schema/aop" 
      xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 

    <aop:aspectj-autoproxy> 
     <aop:include name="logSettersCalls"/> 
    </aop:aspectj-autoproxy> 
    <bean id="logSettersCalls" class="application.logging.aop.LogSettersCalls"/> 

    <bean id="sale01" class="application.common.entities.BidRoom"> 
     <constructor-arg index="0" type="int" value="0001"/> 
     <constructor-arg index="1" type="int" value="15"/> 
    </bean> 
</beans> 

アスペクトロギングクラス:

@Aspect 
public class LogSettersCalls { 
    @Pointcut("execution(void set*(*))") 
    public void setMethod() {} 

    @Before("setMethod()") 
    public void logSetterCall(JoinPoint theJoinPoint) { 
     String methodName = theJoinPoint.getSignature().getName(); 
     Object newValue = theJoinPoint.getArgs()[0]; 
     Object theObject = theJoinPoint.getTarget(); 
     System.out.println("The method " + methodName + " is called on object " 
       + theObject + " with the value " + newValue); 
    } 
} 

私は同じコードを使用して、aopでBeanを作成しています。 Proxy11がapplication.common.entities.SaleRoom

に例外をスローラインをキャストすることはできません$: saleRoom =(SaleRoom)context.getBean(と私は、スレッド "メイン" とjava.lang.ClassCastExceptionで 例外を取得します販売名);

ご協力いただければ幸いです。ありがとう。

答えて

36

SaleRoomクラスはインタフェースを実装していますか? yesの場合、あなたはあなたのコードではインターフェースではなくクラスを使用する必要があります。

ISaleRoom saleRoom = (ISaleRoom) context.getBean(saleName); 

あなたのBeanは、いくつかのインタフェースを実装している場合ので、その後、デフォルトでは春は、このインターフェイスに基づいてプロキシを作成します。ここで

a good article about proxy creation in Spring.

あなたがターゲットクラスのプロキシを作成したい場合にも、あなたが春AOPのためのメカニズムをプロキシ変更することができます。これはhere in reference documentationと記載されています。

+0

私のSaleRoomはスレッドを拡張し、インターフェイスを実装していません。 – Tsikon

+2

私は、問題の理由は、ThreadがRunnableを実装していると思います。アクセスする必要のあるビジネスメソッドを定義する独自のビジネスインターフェイス(ISaleRoomなど)を作成してください。その後、私は答えで述べたように、コード(ISaleRoom)context.getBean(saleName)にキャストします。 – dimas

+0

または、プロキシメカニズムを変更することができます(投稿したリンクを見てください)。 – dimas

関連する問題