2012-11-19 15 views
6

का उपयोग करने के लिए नहीं डाला जा सकता है, मैं बीन्स के माध्यम से वस्तुओं को बनाने के लिए वसंत का उपयोग कर रहा था। अब मैंने एक ही ऑब्जेक्ट बनाने के लिए एओपी का उपयोग करने की कोशिश की है और मुझे $ 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> 

और मैं बिक्री बनाने के लिए निम्नलिखित कोड का इस्तेमाल किया:

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 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); 
    } 
} 

मैं एओपी के माध्यम से सेम बनाने के लिए एक ही कोड का उपयोग कर रहा हूं। $ Proxy11 application.common.entities.SaleRoom

लाइन है कि अपवाद फेंकता में ढाला नहीं जा सकता है:: saleroom = (saleroom) context.getBean (और मैं "मुख्य" java.lang.ClassCastException सूत्र में अपवाद मिल saleName);

किसी भी मदद की सराहना की जाएगी। धन्यवाद।

उत्तर

36

क्या आपकी सेलरूम कक्षा कुछ इंटरफ़ेस लागू करती है? यदि हाँ, तो आप इंटरफ़ेस और न वर्ग आप कोड में उपयोग करना चाहिए:

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

क्योंकि आपके सेम कुछ इंटरफेस तो वसंत डिफ़ॉल्ट इस इंटरफेस के आधार पर प्रॉक्सी बना दिया जाएगा, को लागू करता है।

यहाँ a good article about proxy creation in Spring.

इसके अलावा, आप वसंत AOP के लिए तंत्र को प्रॉक्सी बदल सकते हैं आप लक्ष्य वर्ग के लिए प्रॉक्सी बनाने के लिए चाहते हैं। यह here in reference documentation वर्णित है।

+0

मेरा सेलरूम थ्रेड फैलाता है और किसी भी इंटरफेस को लागू नहीं करता है। – Tsikon

+2

मुझे लगता है कि समस्या का कारण यह है कि थ्रेड लागू करने योग्य लागू करता है। अपना खुद का व्यवसाय इंटरफ़ेस बनाने के लिए प्रयास करें (उदाहरण के लिए ISaleRoom) जहां व्यापार विधियों को परिभाषित करें जिन्हें आपको एक्सेस करने की आवश्यकता है। फिर कोड में इंटरफ़ेस (ISaleRoom) संदर्भ में डाला गया .getBean (saleName) जैसा कि मैंने उत्तर में बताया है। – dimas

+0

या आप प्रॉक्सी तंत्र को बदलने की कोशिश कर सकते हैं (मैंने जो लिंक पोस्ट किया है उसे देखें)। – dimas

संबंधित मुद्दे