View Full Version : Getting instance of a ProxyFactoryBean
arnel
Apr 19th, 2005, 03:02 AM
Hello there!
New to Spring. :D
How do we get instance of a ProxyFactoryBean in java.
The usual way is context.getBean("BeanId").
What if i have a constructor? How do we get the instance. This can be achieve via constructor injection in my config file but i get java.lang.ClassCastException when i call the ProxyFactoryBean in my code.
arnel
[/code]
Rod Johnson
Apr 19th, 2005, 05:57 AM
You want the instance of the factory bean, not the object it creates? You can do this as follows:
context.getBean("&BeanId").
Note the use of the ampersand before the name.[/quote]
arnel
Apr 19th, 2005, 07:42 PM
No sir. I need an object but my object has a constructor that takes a parameter of certain type. I have a doubt that the proxy bean factory does not create an object that has a constructor. What i need to be proxied is an object that takes parameter of a certain type. :)
Take a look at my bean config:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="SpringCustomer" class="arnel.nicolas.Customer">
<property name="firstname">
<value>Arnel</value>
</property>
<property name="lastname">
<value>Nicolas</value>
</property>
</bean>
<bean id="BankTarget" class="arnel.nicolas.Bank">
<constructor-arg index="0">
<ref bean="SpringCustomer"/>
</constructor-arg>
</bean>
<bean id="WithDrawalAdvice" class="arnel.nicolas.BeforeWithdrawal"/>
<bean id="BankPointCutAdvisor" class="org.springframework.aop.support.NameMatchMethodPoi ntcutAdvisor">
<property name="advice">
<ref bean="WithDrawalAdvice"/>
</property>
<property name="mappedName">
<value>withdrawMoney</value>
</property>
</bean>
<bean id="Bank" class="org.springframework.aop.framework.ProxyFactoryBean">
<constructor-arg index="0">
<ref bean="SpringCustomer"/>
</constructor-arg>
<property name="proxyInterfaces">
<value>arnel.nicolas.IBank</value>
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="target">
<ref bean="BankTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>BankPointCutAdvisor</value>
</list>
</property>
</bean>
</beans>
This is my java code:
........
import org.springframework.context.*;
import org.springframework.context.support.FileSystemXmlA pplicationContext;
import org.springframework.aop.framework.ProxyFactoryBean ;
public class SpringBreakMain {
public static void main(String [] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("D:\\SpringBreak\\src\\SpringBreak.xml");
(Bank) bank =(Bank)context.getBean("Bank");
bank.withdrawMoney(2500);
}
}
[/code]
Now if i run the code it spits-out this error:
....................... 1 constructor arguments specified but no matching constructor found in bean 'Bank' (hint: specify index arguments for simple parameters to avoid type ambiguities.
robh
Apr 20th, 2005, 04:13 AM
Try removing the constructor-arg definition from the ProxyFactoryBean and setting proxyTargetClass to false.
Rob
arnel
Apr 20th, 2005, 07:29 PM
Try removing the constructor-arg definition from the ProxyFactoryBean and setting proxyTargetClass to false.
...now i got this error:
.......initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
I am new to Spring so i really don't have any idea about this error. My assumption is that the ProxyFactoryBean did not create my object with constructor that takes a parameter, thats why when i try to invoke in my code using context.getBean("BeanId") i am geting a null object. I think one solution for this is by method-injection instead of constructor-injection bcause the ProxyFactoryBean does not allow me to do this. Correct me if i am wrong. :)
Thanks
robh
Apr 21st, 2005, 02:56 AM
With proxyTargetClass set to false, you only create the object once in its definition so you DO need the <constructor-arg> definitions there. In the ProxyFactoryBean you don't need the <constructor-arg> definition because the proxy is created from your defined interface.
Can you post the full stack trace for this error?
Rob
arnel
Apr 21st, 2005, 06:41 PM
Hi rob !
I got it working already :). All my advice methods are being fired as i expected. Thank you for your support. More power to the Spring Team.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.