Results 1 to 7 of 7

Thread: Getting instance of a ProxyFactoryBean

  1. #1
    Join Date
    Apr 2005
    Posts
    4

    Default Getting instance of a ProxyFactoryBean

    Hello there!

    New to Spring.

    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]

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    You want the instance of the factory bean, not the object it creates? You can do this as follows:
    Code:
    context.getBean("&BeanId").
    Note the use of the ampersand before the name.[/quote]
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Apr 2005
    Posts
    4

    Default

    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:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    	"http&#58;//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.NameMatchMethodPointcutAdvisor">
    		<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:
    Code:
    ........
    
    import org.springframework.context.*;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    import org.springframework.aop.framework.ProxyFactoryBean;
    
    public class SpringBreakMain &#123;
    
    	public static void main&#40;String &#91;&#93; args&#41; &#123;
    		ApplicationContext context = new FileSystemXmlApplicationContext&#40;"D&#58;\\SpringBreak\\src\\SpringBreak.xml"&#41;;
    		&#40;Bank&#41; bank =&#40;Bank&#41;context.getBean&#40;"Bank"&#41;;
    		bank.withdrawMoney&#40;2500&#41;;
    		
    	&#125;
    &#125;
    [/code]
    Now if i run the code it spits-out this error:
    Code:
    ....................... 1 constructor arguments specified but no matching constructor found in bean 'Bank' &#40;hint&#58; specify index arguments for simple parameters to avoid type ambiguities.

  4. #4
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    Try removing the constructor-arg definition from the ProxyFactoryBean and setting proxyTargetClass to false.

    Rob
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  5. #5
    Join Date
    Apr 2005
    Posts
    4

    Default

    Try removing the constructor-arg definition from the ProxyFactoryBean and setting proxyTargetClass to false.
    ...now i got this error:
    Code:
    .......initialization of bean failed; nested exception is org.aopalliance.aop.AspectException&#58; 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

  6. #6
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    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
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  7. #7
    Join Date
    Apr 2005
    Posts
    4

    Default

    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.

Similar Threads

  1. Replies: 4
    Last Post: Oct 5th, 2005, 11:04 AM
  2. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  3. Replies: 6
    Last Post: May 25th, 2005, 01:56 AM
  4. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  5. DefaultAdvisorAutoProxyCreator skipping beans
    By youngm in forum Container
    Replies: 6
    Last Post: Apr 12th, 2005, 04:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •