aspose file tools*
The moose likes Other Application Frameworks and the fly likes Class cast exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Class cast exception" Watch "Class cast exception" New topic
Author

Class cast exception

arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Hello there

i am having problem during the invocation of my proxy bean. It says ....java.lang.ClassCastException.



some of my bean config

any help is highly appreciated. Thanks in advance.

arnel
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Should that one line say IBank instead of Bank?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Hi Lasse,

Thanks for your attention. The IBank there is an interface. AFAIK to create a proxy bean in Spring requires an interface instead of the direct class implementation. The target property of this proxy bean requires the IBank implementation....if i am correct.

during the invocation of this code:

it spit-out an error saying java.lang.ClassCastException

When i change my code to like this:


the error change to: Bean name "Bank" must be of type [arnel.nicolas.Bank],
but was actually of type [$Proxy0].

I have a doubt if Spring created a bean name "Bank" of arnel.nicolas.Bank type.

Anyway thanks for your reply. I will just check their forum.
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
ok guys, i just refactored my code. It seems that there is a problem during the creation of my ProxyFactoryBean Bank. My target class TargetBank has a constructor. So values can be set thru constructor-injection via my bean config file. I removed the constructor and provide a method for it and set the values via method-injection. The code works find and it fires all my advice methods as i expected.

Now, is there a way to get the instance of a ProxyFactoryBean with constructor in the source code? like

Funny huh!?
Ryan Breidenbach
author
Greenhorn

Joined: Jan 06, 2004
Posts: 18
From your original code example, try this instead:



Spring's bean factory will not return an instance of a Bank, but rather a dynamically created object that implements the IBank interface that proxies method invocations to your a Bank object.
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Originally posted by Ryan Breidenbach:
From your original code example, try this instead:



Spring's bean factory will not return an instance of a Bank, but rather a dynamically created object that implements the IBank interface that proxies method invocations to your a Bank object.


Hmm...but my concrete class Bank has a constructor that takes Object of Customer type as a parameter . I am assuming that Spring's bean factory will do create a proxy object with parameter as i have in my Target class (referring to Bank). I tried to change the code basing from your suggestion but the compiler is complaining about error. I have a doubt that the bean factory did not create a proxy bean that takes a constructor of customer type thats why i am having that error.

Here is my bean config:
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Originally posted by Ryan Breidenbach:
From your original code example, try this instead:



Spring's bean factory will not return an instance of a Bank, but rather a dynamically created object that implements the IBank interface that proxies method invocations to your a Bank object.


Hello there Mr. Breidenbach,

Discard my previous posting, you were right . I took your suggestion.The bug there is not during my invocation but it is now in my Advice method. I got it working already.

Just a comment, Spring AOP is nice and pretty straight forward but sometimes they are hard to debug

Thanks a lot!

arnel
sebastian ismael
Greenhorn

Joined: May 13, 2005
Posts: 1
hi arnel, i�m from argentina, sorry for my english !!!

i have the same problem!!! please help me:

ApplicationContext context = new FileSystemXmlApplicationContext("C:\\eclipse3.0\\workspace\\SpringTest\\war\\WEB-INF\\SpringTest-servlet.xml");
Product product = (Product)context.getBean("product") ;

my xml is:

<!-- Bean configuration -->
<bean id="product" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.test.security.SecurityAdvices</value>
</property>
<property name="target">
<ref local="productTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>securityAdvisor</value>
</list>
</property>
</bean>

<!-- Bean Classes -->
<bean id="productTarget" class="com.test.business.beans.Product">
</bean>

<!-- Advisor pointcut definition for after advice -->
<bean id="securityAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="securityAdvice"/>
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean>

<!-- Advice classes -->
<bean id="securityAdvice" class="com.test.aop.BeforeCreateBusinessObjectBeanAdvice"/>

regars !!!

seba
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class cast exception