2012-11-19 2 views
6

bean을 통해 객체를 생성하기 위해 spring을 사용했습니다. 이제는 AOP를 사용하여 동일한 객체를 만들려고했지만 $ Proxy를 SaleRoom 예외로 캐스팅 할 수 없습니다.

이전 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/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입니다 :

<?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을 통해 빈을 만드는 데 같은 코드를 사용하고 있습니다. $ Proxy11이 application.common.entities.SaleRoom

예외가 발생합니다 라인으로 캐스팅 할 수 없습니다 : saleRoom = (SaleRoom) context.getBean을 (내가 "기본"java.lang.ClassCastException가 스레드에서 예외를 얻을 saleName);

도움이 될 것입니다. 감사.

답변

36

SaleRoom 클래스가 일부 인터페이스를 구현합니까? 예, 당신은 당신의 코드에서 인터페이스가 아닌 클래스를 사용해야하는 경우 :

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

가 있기 때문에 빈은이 인터페이스를 기반으로 프록시를 생성합니다 기본적으로 다음 몇 가지 인터페이스 봄을 구현하는 경우.

다음은 대상 클래스에 대한 프록시를 만들려면

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

관련 문제