Announcement Announcement Module
Collapse
No announcement yet.
Injecting the return value of a method into a bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting the return value of a method into a bean

    Greetings

    I'm new to spring. Im amazed at how easy Spring makes everything. Now to the question. I need to be able to dynamically set the value of wsdlDocumentUrl. I wanted to create a bean that constructs the url and inject it into my TRMSReadinessService bean. Is that possible? The bean would have a getWsdlDocumentUrl method that would return the URL to the web service.

    So can you inject the return value from a bean?

    Thanks in advance
    Derrick

    The following is the bean that I want to inject into.
    PHP Code:

        
    <bean id="TRMSReadinessService" class="RASWEB.webservice.TRMS.src.TRMSPortProxyFactoryBean">
            <
    property name="serviceFactoryClass">
                <
    value>org.apache.axis.client.ServiceFactory</value>
            </
    property>
            <
    property name="wsdlDocumentUrl">
                <
    value>http://localhost:7001/SwnnWebServices/V1_0/Services/TEST.jws?WSDL</value>
            
    </property>
            
            <
    property name="namespaceUri"
                <
    value>http://www.openuri.org/</value>
            
    </property>
            
            <
    property name="serviceName">
                <
    value>TEST</value>
            </
    property>
            <
    property name="portName">
                <
    value>TESTSoap</value>
            </
    property>
            <
    property name="portInterface">
                <
    value>RASWEB.webservice.TRMS.axis.TESTSoap</value>
            </
    property>
            
            <
    property name="serviceInterface">
                <
    value>RASWEB.webservice.TRMS.src.TestService</value>
            </
    property>
        </
    bean

  • #2
    threre are 4 ways (roughly):
    1. you can use PropertyPlaceholderConfigurer and store your URL in an external .properties file (not exactly what you need, but very simple )
    2. Use MethodInvokingFactoryBean
    3. Use your url-generating class as a factory and call it's factory method: static or instance's.
    4. Implement the FactoryBean interface for the url-generating class, though you'll introduce the dependency on Spring.

    Comment


    • #3
      Thank Injecteer

      I'm going to try your option 2

      Derrick

      Comment


      • #4
        Originally posted by derrickfn
        I need to be able to dynamically set the value of wsdlDocumentUrl.
        Can you clarify "dynamic". Do you mean it can change at run time (e.g. specific to each logged on user)?

        Originally posted by derrickfn
        I wanted to create a bean that constructs the url and inject it into my TRMSReadinessService bean. Is that possible? The bean would have a getWsdlDocumentUrl method that would return the URL to the web service.
        Injecteers already answered this, very nicely as well One thing I would add is I am not sure whether Spring can convert from Strings to URLs. You might need to write your own propertyEditor (if you are using the propertyPlaceHolder suggestion). Checkout http://springframework.org/docs/refe...-customeditors (3.14)

        Originally posted by derrickfn
        So can you inject the return value from a bean?
        A resounding yes; you most certainly can

        Comment


        • #5
          Originally posted by yatesco
          One thing I would add is I am not sure whether Spring can convert from Strings to URLs.
          why not?

          the only issue, that can appear here is, that the default URLEditor might be servlet-context-related, so you'll have to expliciltly register org.springframework.beans.propertyeditors.URLEdito r in your context.

          Comment


          • #6
            Originally posted by Injecteer
            why not?
            I was wondering whether Spring already had a registered URLPropertyEditor....
            Originally posted by Injecteer
            the only issue, that can appear here is, that the default URLEditor might be servlet-context-related, so you'll have to expliciltly register org.springframework.beans.propertyeditors.URLEdito r in your context.
            .....which you thenanswered

            Comment


            • #7
              Sorry for the long delay,

              I was trying to get the rest of the application working.

              Injectors suggest #2 worked without any problems. MY method returned a String with the Url. This application is part of a government delivery. The application will be delivered before I know what the actual end point is. Due to the delivery/installation requirements, I can not make any changes to the application contained in the war files. However, I can make changes to items stored in a database.

              Thanks for all of the suggestions
              Derrick

              Comment


              • #8
                I also have a similar requirement. I want to set wsdl url and web service end point at the run time.

                I am a newbie and not really familiar with MethodInvokingFactoryBean. Can you please paste an example on how this could be done.

                Thanks.


                Originally posted by derrickfn
                Sorry for the long delay,

                I was trying to get the rest of the application working.

                Injectors suggest #2 worked without any problems. MY method returned a String with the Url. This application is part of a government delivery. The application will be delivered before I know what the actual end point is. Due to the delivery/installation requirements, I can not make any changes to the application contained in the war files. However, I can make changes to items stored in a database.

                Thanks for all of the suggestions
                Derrick

                Comment


                • #9
                  There is an example of usage of MethodInvolingFactoryBean in the API docs. See also PropertyPathFactoryBean - might also suit your needs.

                  Comment


                  • #10
                    Originally posted by parmod
                    I am a newbie and not really familiar with MethodInvokingFactoryBean. Can you please paste an example on how this could be done.
                    An example of the usage of MethodInvokingFactoryBean can be found in the API documentation of that class.

                    Regards,
                    Andreas

                    Comment


                    • #11
                      I want to inject the endpoint to the proxy bean, which is the result of a method call of another bean

                      Here is a class with a method returing the endpoint

                      Code:
                        public class BusinessUnitClient {
                      	public String getEndPointUrl(){
                      		return "http://localhost:8080/bu-test/services/buwebservices";
                      	
                                 }
                      
                      
                        }

                      Here is the proxy bean that needs this returned endpoint url:

                      Code:
                      public class BusinessUnitClientProxyFactoryBean extends	JaxRpcPortProxyFactoryBean {
                      	
                      	private String m_endPoint;
                      
                      	protected void postProcessJaxRpcService(Service pService) {
                      	        setEndpointAddress(m_endPoint);
                      	}
                      	
                          
                      	public void addEndpoint(String args){
                      		m_endPoint = args;
                      	}
                      
                      }
                      Here is the configuration
                      Code:
                      	<bean id="MyBusinessUnit" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                      	  <property name="targetObject">
                      	  	<ref bean="IBusinessUnitClient"/>
                      	  </property>
                      	  <property name="targetMethod" value="getEndPointUrl"/>
                      	</bean>
                      
                      	<bean id="MyBusinessUnitProxy" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                      	  <property name="targetObject">
                      	  	<ref bean="RequestAffiliateeWebService"/>
                      	  </property>
                      	  <property name="targetMethod" value="addEndpoint"/>
                      	  	  <property name="arguments">
                      	  		<list>
                      	  			<ref bean="MyBusinessUnit"/>
                      			</list>
                      	  </property>	
                      	</bean>


                      I get this exception:

                      Code:
                      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyBusinessUnitProxy' defined in file [C:\workspace\affiliate\dist\assemble\config\spring\affiliate\affiliate-webservice.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodException: $Proxy1.addEndpoint(java.lang.String)
                      java.lang.NoSuchMethodException: $Proxy1.addEndpoint(java.lang.String)	at java.lang.Class.getMethod(Unknown Source)
                      	at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:180)
                      	at org.springframework.beans.factory.config.MethodInvokingFactoryBean.afterPropertiesSet(MethodInvokingFactoryBean.java:111)
                      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:856)
                      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:825)
                      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:418)
                      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:241)
                      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:152)
                      	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:247)
                      	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:331)
                      	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:89)
                      	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:74)
                      	at com.netbet.affiliate.facade.BusinessUnitClientTest.testRequestAffiliatee(BusinessUnitClientTest.java:51)
                      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                      	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                      	at java.lang.reflect.Method.invoke(Unknown Source)
                      	at junit.framework.TestCase.runTest(TestCase.java:154)
                      	at junit.framework.TestCase.runBare(TestCase.java:127)
                      	at junit.framework.TestResult$1.protect(TestResult.java:106)
                      	at junit.framework.TestResult.runProtected(TestResult.java:124)
                      	at junit.framework.TestResult.run(TestResult.java:109)
                      	at junit.framework.TestCase.run(TestCase.java:118)
                      	at junit.framework.TestSuite.runTest(TestSuite.java:208)
                      	at junit.framework.TestSuite.run(TestSuite.java:203)
                      	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
                      	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
                      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
                      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
                      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
                      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
                      I am using Spring RC2. Not sure what's the issue

                      Thanks.


                      Originally posted by Andreas Senft
                      An example of the usage of MethodInvokingFactoryBean can be found in the API documentation of that class.

                      Regards,
                      Andreas

                      Comment


                      • #12
                        MethodInvokingFactoryBean vs. @factory-bean

                        Originally posted by Andreas Senft
                        An example of the usage of MethodInvokingFactoryBean can be found in the API documentation of that class.
                        Might be a bit off-topic here, but what's the difference between MethodInvokingFactoryBean and the usage of @factory-bean and @factory-method?

                        An example which I use:
                        Code:
                        <bean factory-bean="myFactory" factory-method="getInstance">
                          <constructor-arg value="propertyValue"/>
                        </bean>
                        Jörg

                        Comment


                        • #13
                          I am not trying to create a bean. Beans have already been created by the bean factory. I am trying to inject the value returned by a method to another bean.

                          What was your point?

                          Thanks.


                          Originally posted by Jörg Heinicke
                          Might be a bit off-topic here, but what's the difference between MethodInvokingFactoryBean and the usage of @factory-bean and @factory-method?

                          An example which I use:
                          Code:
                          <bean factory-bean="myFactory" factory-method="getInstance">
                            <constructor-arg value="propertyValue"/>
                          </bean>
                          Jörg

                          Comment


                          • #14
                            Originally posted by Jörg Heinicke
                            Might be a bit off-topic here, but what's the difference between MethodInvokingFactoryBean and the usage of @factory-bean and @factory-method?
                            Jörg
                            If you mean the factory-method attributes in XML, then nothing; they are equivalent.

                            The factory-class element (instead of factory-bean) allows you to call a method on a class, as oppose to an instance of a class.

                            Comment


                            • #15
                              How would I use this to inject value returned by the method from another bean?

                              Originally posted by yatesco
                              If you mean the factory-method attributes in XML, then nothing; they are equivalent.

                              The factory-class element (instead of factory-bean) allows you to call a method on a class, as oppose to an instance of a class.

                              Comment

                              Working...
                              X