Announcement Announcement Module
Collapse
No announcement yet.
Getting instance of a ProxyFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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]

    Comment


    • #3
      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.

      Comment


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

        Rob

        Comment


        • #5
          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

          Comment


          • #6
            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

            Comment


            • #7
              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.

              Comment

              Working...
              X