Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class cast exception

 
arnel nicolas
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should that one line say IBank instead of Bank?
 
arnel nicolas
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic